Search code examples
rhino-mocks

What happened to Rhino Mocks' Arg<T>.Property


Arg<T>.Property is part of the documentation on inline constraints for Rhino Mocks v3.5, but I can't find it in v.3.6. What happened?

The documentation is here.

and Arg<T>.Property is mentioned in the constraints reference table.


Solution

  • This seems to be a bug in the documentation. When examining the Rhino.Mocks.dll (3.6.0.0) in the Object Browser I see that Rhino.Mocks.Arg<T> only offers the methods Is and List but not Property.

    However Rhino.Mocks.Constraints contains the Property class. Using the "old" syntax you should be able to do the same:

    AAA syntax (producing compile error):

    myStub.Expect(x => x.MethodToCall(Arg<T>.Property.Value("PropertyName", myDesiredPropertyValue))).Result(myMockResult);
    

    Old syntax (working):

    myStub.Expect(x => x.MethodToCall(null)).Constraints(Property.Value("PropertyName", myDesiredPropertyValue)).Result(myMockResult);
    

    The documentation says "You're probably used to IgnoreArguments(), Constraints() and RefOut(). [...] It is encouraged to use only Arg<T>, it is more consistent and easier to understand, even if sometimes a little more to write."

    As Jeff Bridgman noted, you can also use Arg<T>.Matches:

    myStub.Expect(x => x.MethodToCall(Arg<T>.Matches(m => m.PropertyName == myDesiredPropertyValue))).Result(myMockResult);
    

    It has the advantage of being 'refactory safe', meaning that you can refactor the name of the property safely without the need to search for any 'magic strings'. It also meets the suggestion in the documentation to rather use Arg<T> instead of Constraints().