captncraig
captncraig

Reputation: 23068

AssertWasCalled in rhino mocks

I have an object under test that makes a fairly complicated call to a data access object. IT looks something like

object.DoSomething(somestring,someObject,someOtherObject,someOtherOtherObject)

In my test structure I have a mocked version of object and I want to test that Dosomething got called with somestring == "value1" and someObject.porpertyA == "value2".

I can't use the simple AssertWasCalled() overload because I don;t know about (or care about) someOtherObject. I notice another overload that takes an an action for setup constraints, but I've never seen it used.

Upvotes: 7

Views: 4491

Answers (2)

Andriy Volkov
Andriy Volkov

Reputation: 18923

Piece of cake:

yourstub.AssertWasCalled(
             x => x.DoSomething(
                Arg<string>.Is.Equal("value1"), 
                Arg<someObjectType>.Is.Equal(value2), 
                Arg<someOtherObjectType>.Is.Anything,   <======== NOTE THIS!
                Arg<someOtherOtherObjectType>.Is.Equal(value3)
             )
);

Upvotes: 19

Jon Skeet
Jon Skeet

Reputation: 1500225

Have a look at the documentation for constraints.

I suspect you want:

Expect.Call(object.DoSomething(null, null, null, null)
      .IgnoreArguments() // Ignore those nulls
      .Constraints(Is.Equal("value1"),
                   Property.Value("PropertyA", "value2"),
                   Is.Anything(),
                   Is.Anything())
      .Return(whateverItShouldReturn);

Upvotes: 4

Related Questions