I finally got around to downloading and playing with the forthcoming release of Rhino Mocks v3.5 (more info here from Ayende).
The major changes to this version is that the syntax has been revamped to allow for the new language features in .Net 3.5. Another big change with this release is that the dreaded Record-Replay semantics have been depreciated (although I have to admit that they never bothered me).
What I thought I would do today is do a little comparison with Rhino 3.5 against Rhino 3.4 (and older) as well as compare it to Moq as this mocking framework has been getting a lot of buzz lately.
The example below is a pretty straight forward example where I need to mock out my data repository for a test and I want to ensure that the correct call is actually made.
Rhino 3.5 Syntax
var admin = new Admin();
MockRepository mockRepository = new MockRepository();
var adminRepository = mockRepository.StrictMock< IAdminRepository >();
adminRepository.Expect( x => x.GetSiteTotals() ).Return( new SiteTotals() );
ObjectFactory.InjectStub( typeof( IAdminRepository ), adminRepository );
var siteTotals = admin.GetSiteTotals();
... Asserts here....
mockRepository.VerifyAllExpectations();
Rhino 3.4 and older syntax
MockRepository mockRepository = new MockRepository();
var admin = new Admin();
var adminRepository = mockRepository.CreateMock();
using ( mockRepository.Record() )
{
Expect.Call( adminRepository.GetSiteTotals() ).Return( new SiteTotals() );
}
ObjectFactory.InjectStub( typeof( IAdminRepository ), adminRepository );
using ( mockRepository.Playback() )
{
var siteTotals = admin.GetSiteTotals();
... Asserts here....
mockRepository.VerifyAll();
}
Moq Syntax
var admin = new Admin();
var mockAdminRepository = new Mock< IAdminRepository >();
mockAdminRepository.Expect( x => x.GetSiteTotals() ).Returns( new SiteTotals() );
ObjectFactory.InjectStub( typeof ( IAdminRepository ), mockAdminRepository.Object );
var siteTotals = admin.GetSiteTotals();
... Asserts here....
mockAdminRepository.VerifyAll();
Rhino 3.5 compared to older versions of Rhino
Lets take a quick look at the syntax differences between the new version and the old.
First thing you will notice with 3.5 is that it removes the need to explicitly perform a record/replay. I personally never had an issue with this syntax, but it does remove noise from your code and this is a good thing.
Next you may notice that there is no more need to use the Expect object to set your expectations. With 3.5 you can simply use the Extension Method Expect and it will do the same thing (love this).
Lastly take notice to how I am calling mockRepository.VerifyAllExpectations() in place of mockRepository.VerifyAll(). From what I can tell this is just a naming change for clarity sake, but I like it.
Rhino 3.5 compared to Moq
Lets take a quick look at the syntax differences between Rhino 3.5 and Moq.
Like the new version of Rhino you do not need to implement the Record/Replay semantics when creating your tests, and this is good as I said above because it removes noise.
First you will notice that with Moq u create all your objects via the 'Mock' object were with Rhino you use the MockRepository. They both do the same thing, however with Moq the object you get back is NOT the actual type you requested, it is a wrapper around your type that is used for testing. Personally I find Moq's syntax a little annoying and clumsy.
Next take notice that when you want to get access to the actual type instance of your mock you have to do mockAdminRepository.Object. This is fine, but is just one more thing I have to remember to do. I am always forgetting to add the .Object and my test will fail because of it.
Last, because Rhino still creates all the mocks out of the MockRepository you are able to call mockRepository.VerifyAllExpectations() one time to ensure that all expectations where met. However, with Moq you need to call mockAdminRepository.VerifyAll() on each mock object (if there is a better way in Moq, please let me know). To me having to potentially call .VarifyAll() multiple times just adds more noise to my test.
Now that I have spent a little time playing with Rhino 3.5, I have to say I am hooked. In fact, I think i am going to gut my tests for DimeCasts.net that were originally written in Moq and replace them with Rhino 3.5.
Till next time,
[----- Remember to check out DimeCasts.Net -----]
Posted
06-27-2008 8:23 AM
by
Derik Whittaker