views:

341

answers:

2

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. Any advice would be great.

+1  A: 

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);
Jon Skeet
+5  A: 

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)
             )
);
zvolkov
Yey! I just beat Jon Skeet!!! :)
zvolkov
Thaat works greaat. The only other thing I found to be useful was to use Arg<someObjectType>.Matches(y => y.property == whatever); for checking values on the argument objects.
CaptnCraig
Yup, I know that, just didn't show, for simplicity. Enjoy!
zvolkov