2013-07-08 2 views
4

저는 Rhino Mocks 3.5를 사용하여 2 개의 매개 변수를 취하는 서비스 메소드 호출을 조롱했습니다. 객체의 프라 티티가 올바르게 설정되었는지 확인하려고합니다.Rhino 모의 파라미터 검사 ... 더 좋은 방법이 있습니까?

// Method being tested 
void UpdateDelivery(Trade trade) 
{ 
    trade.Delivery = new DateTime(2013, 7, 4); 
    m_Service.UpdateTrade(trade, m_Token); // mocking this 
} 

여기 (작동) 내 코드

service, trade, token declared/new'd up ... etc. 
... 

using (m_Mocks.Record()) 
{ 
    Action<Trade, Token> onCalled = (tradeParam, tokenParam) => 
      { 
       // Inspect/verify that Delivery prop is set correctly 
       // when UpdateTrade called 
       Assert.AreEqual(new DateTime(2013, 7, 4), tradeParam.Delivery);      
      }; 

    Expect.Call(() => m_Service.UpdateTrade(Arg<Trade>.Is.Equal(trade), Arg<Token>.Is.Equal(token))).Do(onCalled); 
} 

using (m_Mocks.Playback()) 
{ 
    m_Adjuster = new Adjuster(service, token); 
    m_Adjuster.UpdateDelivery(trade); 
} 

코뿔소 모의 객체를 사용하여이를 테스트 할 수있는 더 나은, 더 간결 straightfoward 방법이 있나요의 일부인가? Contraints가 사용되는 게시물을 보았지만 문자열 이름별로 속성/값을 식별하는 팬이 아닙니다.

답변

5

당신은 다음을 수행 할 수 있습니다 : 당신은 당신이 그것을 위해 Is.Anything 제약 조건을 사용할 수 있습니다,이 테스트에서 token 매개 변수의 유효성을 검사하지 않을 경우

Expect.Call(() => m_Service.UpdateTrade(
    Arg<Trade>.Matches(t => t.Delivery.Equals(new DateTime(2013, 7, 3))), 
    Arg<Token>.Is.Anything) 
); 

또한, 유의하시기 바랍니다.


참고 :

RhinoMocks Matches(Expression<Predicate<..>>) 오버로드를 사용하는 경우 3.5 .NET4 +는 AmbiguousMatchException을 던져. 이 RhinoMocks 3.6로 업데이트 할 수없는 경우 (이유가), 하나는 여전히 그래서 Matches(AbstractConstraint) 과부하를 사용할 수 있습니다

Arg<Trade>.Matches(
    Property.Value("Delivery", new DateTime(2013, 7, 3))) 

나 :

Arg<Trade>.Matches(
    new PredicateConstraint<DateTime>(
    t => t.Delivery.Equals(new DateTime(2013, 7, 3)))) 
+1

감사 알렉산더. 이것은 내가 찾고 있었던 바로 그 것이다. "System.Reflection.AmbiguousMatchException : 모호한 일치를 발견했습니다." 내가 달릴 때. .Net 4.0을 사용하고 있습니다. – KornMuffin

+0

어떤 예외가 발생합니까? –

+0

테스트중인 클래스 내에서 m_Service.UpdateTrade 메서드가 호출 될 때 Rhino Mock에 의해 발생합니다. System.Reflection.AmbiguousMatchException : 모호한 일치가 발견되었습니다. System.RuntimeType.GetMethodImpl에서 \t (문자열 이름, BindingFlags의 bindingAttr 바인더 바인더 CallingConventions callConv, 유형 [] 유형 ParameterModifier [] 개질제) Rhino.Mocks에서 System.Type.GetMethod (문자열 이름) \t에서 \t. Constraints.LambdaConstraint.Eval (오브젝트 OBJ)에 Rhino.Mocks.Expectations.ConstraintsExpectation.DoIsExpected \t (개체 [] 인수) Rhino.Mocks.Expectations.AbstractExpectation.IsExpected에서 \t (개체 [] 인수) ... – KornMuffin