2011-08-09 2 views
20

RhinoMocks를 사용하여 속성의 getter 값을 스텁하려고합니다. 이 속성은 getter 액세스 만있는 인터페이스의 일부로 정의됩니다.속성을 스텁하기 RhinoMocks를 사용하여 가져 오기

"잘못된 호출, 마지막 호출이 사용되었거나 호출이 생성되지 않았습니다. (가상 (C#)/대체 가능 (VB) 메서드를 호출하고 있는지 확인하십시오.)"오류가 발생합니다. 나는 이것이 스터 빙 (stubbing)하는 부동산이 가상이 아니라는 것을 의미 할 수도 있다는 것을 이해합니다; 그러나 그것은 인터페이스의 일부이며, 그것이 내가이 에러를 얻는 이유인지 확실하지 않습니다.

다음은 코드 스켈레톤입니다. "stubRepository.Stub (x => x.StoreDeviceID) .PropertyBehavior();"라는 줄의 주석 처리를 제거하면 "읽기/쓰기가 가능해야합니다"라는 새로운 오류가 발생합니다. 나는 SO 검색을했고 this 페이지를 찾았습니다. 그러나 제안 된 해결책은 도움이되지 않습니다. 이견있는 사람?

스텁과 일반적으로
stubRepository.Stub(x => x.StoreDeviceID).Return("test"); 

, 속성은 일반 C#을 속성과 같이 사용됩니다 :이 읽기 전용 속성이기 때문에

public interface IStore { 
     string StoreDeviceID {get;} 
     //other methods 
    } 

    public static class Store { 
     private IStore Repository; 

     public void SetRepository(IStore rep){ 
      Repository = rep; 
     } 

     public StoredeviceID { 
      get{ 
       return Repository.StoreDeviceID; 
      } 
     } 

     //other methods 
    } 

    public class TestClass { 
     [Test] 
     public void TestDeviceID() { 
      var stubRepository = 
       MockRepository.GenerateStub<IStore>(); 
      Store.SetRepository(stubRepository); 

      //stubRepository.Stub(x => x.StoreDeviceID).PropertyBehavior(); 
      SetupResult.For(stubRepository.StoreDeviceID).Return("test"); 

      Assert.AreSame(Store.StoreDeviceID, "test"); 
     } 
    } 

답변

28

, 당신은 말할 필요. 그래서 비 읽기 전용 속성, 당신은 말할 것입니다 : stubRepository.someProperty = "test";

은 또한 당신이 상관없이 모의 또는 스텁 여부의 특정 방식으로 작동하는 방법을 설정하기를 원한다면, 당신은 항상 말을 참고 :

stubRepository.Stub(x => x.someMethod()).Return("foo"); 

스텁은 자신의 필요에 의존하여 단위 테스트가 공급하지만,에 검증을 실행하기가 하지입니다 기억; 그것이 바로 mock입니다.

특정 방식으로 작동하는 종속성을 제공하려면 스텁을 사용하십시오. 특정 의존성이 올바르게 상호 작용되었는지 확인하려면 Mock을 사용하십시오. 제 (뛰어난)에서

Rhino Wiki는 :

A mock is an object that we can set expectations on, and which will verify that the expected actions have indeed occurred. A stub is an object that you use in order to pass to the code under test. You can setup expectations on it, so it would act in certain ways, but those expectations will never be verified. A stub's properties will automatically behave like normal properties, and you can't set expectations on them.

If you want to verify the behavior of the code under test, you will use a mock with the appropriate expectation, and verify that. If you want just to pass a value that may need to act in a certain way, but isn't the focus of this test, you will use a stub.

IMPORTANT: A stub will never cause a test to fail.

+0

@ Adam이 속성은 읽기 전용이므로 설정할 수 없습니다. 그러나 Stub에 제공 한 코드 스 니펫이 완벽하게 작동합니다. 바보 같은 저를 제외한 모든 다른 옵션을 시도했습니다 :) – Santhosh

+0

StoreDeviceID에는 setter가 없으므로 첫 번째 명령문 인'stubRepository.StoreDeviceID = "test";'가 작동하지 않습니다. –

+0

아 - 죄송합니다. 내 답변을 업데이트 할 예정입니다. 다행 이군요. –

4

당신은 그루터기와 다음을 수행 할 수

stubRepository.Stub(x => x.StoreDeviceID).Return("test"); 

이는 StoreDeviceID의 게터에 대한 호출에 대해 "테스트"를 반환하게됩니다.

관련 문제