Rhino Mock Parameter Inspection ... Is there a better way?

I use Rhino Mocks 3.5 to make fun of a service method call that takes 2 parameters, and I want to make sure the object property is set correctly.

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

Here is the part of my code (which works)

 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 ); } 

Is there a better, more concise, direct way to test this with Rhino Mocks? I have seen posts that use Contraints, but I'm not a fan of identifying properties / values ​​by string names.

+4
source share
1 answer

You can do the following:

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

Also note that if you are not going to check the token parameter in these tests, you can use the Is.Anything constraint for it.


Note:

RhinoMocks 3.5 and .NET4 + raise an AmbiguousMatchException when using the Matches(Expression<Predicate<..>>) overload Matches(Expression<Predicate<..>>) . If it is impossible to upgrade to RhinoMocks 3.6 (for reasons), you can still use the Matches(AbstractConstraint) overload Matches(AbstractConstraint) as follows:

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

or

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

Source: https://habr.com/ru/post/1490353/


All Articles