Just trying to make sense of things...

Rhino Mock 3.5 Release Candidate - I Got To Help!

Monday, 30 June 2008 02:22 by jordan.terrell

Rhino Mocks 3.5 just went to release candidate.  Probably the biggest change is the new Arrange-Act-Assert (or AAA) syntax.  This now allows you to use a nearly identical approach to Moq's API.  What I like about Rhino Mocks is the fact that it uses extension methods, so you don't have to access an "Object" property to get the real mocked object.

But what is even cooler about this release, at least for me, is that I got to help with it!  I was migrating a project over to Rhino Mocks 3.5 beta, specifically because I wanted to make use of the new AAA syntax to simplify our tests, and during that process I found a pretty big bug - ordered expectations did not work with the new syntax.

To make the problem summary short, when you used the Expect() extension method, it put your mock object in Record mode, recorded the expectation, and then immediately put the mock object back in Replay mode.  Two problems with this: First, it assumed you were in Replay mode to begin with (Ayende noticed/fixed that); Second, switching back to Replay mode originally would ensure that you could not do so within the context of an Ordered() scope.  This was, I assume more to encourage proper use of the API than a real design/implementation constraint.  With the fix that I made in place, IF you were in Replay mode when you call the Expect() extension method, it now skips the check to see if you are in an Ordered() scope.  However, using the previously existing MockRepository.Replay() method still behaves the way it did before - it will throw an exception if you try to call it within an Ordered() scope.  The reason we can make an exception for the extension method is because it is an atomic action: if you were in Replay mode already, you will just be adding an expectation; you'll still end up in Replay mode.  If you were not in Replay mode, it will not try to place you in replay mode.  The API with the fix in place still protects the invariant disallowing you to enter an Ordered() scope in Record mode and switching into Replay mode before exiting the Ordered() scope.

If you want to look at my fix, it's revision 1482 in the Rhino Mocks Subversion repository.  Not a rocket science-like fix, but it's still cool that I got to contribute, and that Ayende choose to recognize my contribution when announcing the Release Candidate (thanks Ayende!).

You can bet I will be updating my project to use the Release Candidate later today...

Categories:   .NET | Programming
Actions:   E-mail | | Permalink | Comments (1) | Comment RSSRSS comment feed