Rhino Mocks 2.1

time to read 3 min | 427 words

I've updated Rhino Mocks, this time it's a feature adding release rather than a bug fix release.

The issue is very simple, previously, when you wanted to set an expectaion on a method with a return value you needed to to this:

Expect.On(projectView).Call(projectView.Title).Return("ayende");

This is very nice way to do things, but it is does violate the DRY prinicpal. In this case, there are some valid reasons for this syntax. Considerring that Expect is a static class, I don't have any context to rely on here. I needed some way to get the proxy instance that called the last method. But I can't get it from the Call() method, since what I get from that is the return value and not the proxy object itself.

The desired thing is to drop the first On(projectView) method call entirely, since in most cases I can just get the last method call for any proxy and that would be the Right Thing to do. Unfortantely, I fear that I let the fear of programmer abuse to come in the path of the common use case. James Robertson talks about it quite often, but I didn't think that I woulf fall for it. I just got a very well written request Geert (no blog) about this which caused me to smack my hand to my forehead in shame.

Therefore, from now on, you can write:

Expect.Call(projectView.Title).Return("ayende");

Now we retain the DRY pricipal :-)

Some words of caution, though. This is implemented using static variable which is shared across all repositories and, of course, all threads. Do not try to use it in multi threaded scenario (the Xml comment state so explicitly). There shouldn't be any confusion in a mutli repository cases as long as you don't try to cheat on purpose.

That said, this is certainly a most common case and it's a very likely that it'll be an improvement for Rhino Mocks users. The repostiory and the download page has been updated.

Happy mocking :-)