Rhino Mocks Constraints In VB.Net

Standard

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:

loTransLog.RecordUpdate(Nothing,Nothing)
LastCall.IgnoreArguments()

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:

loTransLog.RecordUpdate(Nothing,Nothing)
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: 

loTransLog.RecordUpdate(Nothing,Nothing)
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

Advertisements

One response »

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s