I have been using Rhino Mocks for a while now, but I must admit there are a lot of features I know I could use that I don’t. One such feature formerly in this category is Constraints – namely, making my own.
I have a complicated, almost integration test, where I need a repository to return a large set of data to be using by the system under test. Essentially, the SUT will ask for prices for a bunch of securities on two separate dates.
This will always return the same set of prices. I need the Mock to return a different set of prices for a different date. I need to add a constraint:
This will tell Rhino Mocks to return T-N prices when the SUT asks for T-N, and T prices when asked for T.
Unfortunately, the SUT has a problem now where T is GetDate(), so the test will fail sometimes when the time between setting the expectation and the time the prices are asked for is greater than the minimum resolution of DateTime. This will be fixed later, but first I need to get it under test.
What I really want is just to be Date equal anyway, not DateTime.
It’s very easy to add a new constraint for this in Rhino Mocks:
Now I can simply use this constraint to set expectations:
I didn’t consult any documentation, blogs, google, other people to do this. I just let intellisense be my guide and literaly built it in about 2 minutes. I’d never written a custom constraint before. In fact, looking through the Rhino Mocks source code, I now see I could have written a PredicateConstraint instead (but it would have been a little more verbose to use.)
It’s really easy and natural, the way things ought to be (Thanks, Ayende!)