Reputation: 2957
I'm using Rhino Mocks 3.5 to mock a service method call which takes 2 parameters and I want to make sure a propery on an object is being 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's a portion 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, straightfoward way to test this using Rhino Mocks? I've seen posts where Contraints are used but I'm not a fan of identifying properties / value by string names.
Upvotes: 4
Views: 2421
Reputation: 6408
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)
);
Please also note, if you are not going to validate token
parameter in this tests, then you can use Is.Anything
constraint for it.
Note:
RhinoMocks 3.5 and .NET4+ throw an AmbiguousMatchException when using the Matches(Expression<Predicate<..>>)
overload. If it is not possible to update to RhinoMocks 3.6 (there are reasons), one can still use the Matches(AbstractConstraint)
overload as so:
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))))
Upvotes: 6