Category Archives: Rhino Mocks

Rhino Mocks Constraints In VB.Net


Over the past few months I’ve been getting into the Rhino Mocks mocking framework. 

A timesaver for me has been the call “LastCall.IgnoreArguments”.  This has been handy because many of the parameters in my calls are objects that I can’t really verify.

For Example:


Although this is good in that I can keep working, I have found this a little annoying as sometimes a couple of the arguments to my function are verifiable, yet I’m calling “IgnoreArguments”.  In the case above, the first parameter is a string which is checkable.

Ayende has come to the rescue for people who use IgnoreArguments with the concept of “Constraints”.    Constraints allow you to verify arguments individually.

In the example cited, I can verify the first parameter like this:

LastCall.IgnoreArguments().Constraints(Constraints.Is.Equal("foo"), Constraints.Is.Anything)

There are lots of checks you can do besides equality.   I can even improve the above example by ensuring the second parameter (an object) is of an expected type: 

LastCall.IgnoreArguments().Constraints(Constraints.Is.Equal("foo"), Constraints.Is.TypeOf(GetType(TheTypeIWantThisToBe)))

Read about all the other constraints available here:

Rhino Mocks Constraints – Ayende @ Wiki