Why would I select Moles as my mocking framework?
Asked Answered
A

3

9

I've been looking at several Mocking frameworks for ASP.NET and came across Microsoft Moles. This seems to be a part of Microsoft Research team and was wondering If anyone here has selected Moles over other matured Mocking frameworks such as Moq.

Avionics answered 14/6, 2010 at 2:58 Comment(0)
D
15

I actually use Moq and Moles in the same test project. Both have strengths and I use each where appropriate. Generally, I use Moq for the standard sort of AAA testing with verification, and Moles is the 'big gun' for the otherwise unmockable things, like extension method calls etc.

I like this arrangement, because each test can be as simple and sensible as possible, even though the mocking setup might vary a lot from test to test.

Daley answered 21/6, 2010 at 5:39 Comment(4)
+1 Important point that both can be used together - you just need to choose your 'mocker of first resort' carefully.Bagman
would be great to see an example of this, any blog post on your workflow? I find it a bit unatural for TDD tbhPreciosa
You are right that it deserves an example - I don't have one at the moment, but I'll look around and see if any code I have could be turned into one.Daley
Here's an NUnit test using Moles: github.com/fschwiet/Using_Moles_with_NUnit/commit/…Rodriquez
C
5

Moles was designed to work efficiently with the white box analysis of Pex. All other mock framework usually incur a lot of overhead.

Moles provides a simple value proposition: replacing any .NET method with a delegate. By design, Moles does not provide any API to express 'verification' as other frameworks do. It is really up to you to decide whether this decision suits your or not.

If you need to deal with (legacy) code that depends on hard-coded static methods or sealed types with internal constructors, Moles can help you deal with these cases.

If you have interfaces and nicely componentized code, Moles also generates slim stubs, i.e. interface implementation, that you can use with the profiler.

Churchwoman answered 19/6, 2010 at 3:19 Comment(1)
+1 If you want to see how many times a method was called in Moles, you have to put in your own counter and have the delegate call the original method (if desired). Contrast that with Times.Once and other methods provided by some frameworks (like Moq).Yanez
B
3

Moles is more often compared/contrasted to TypeMock in that it offers a set of facilities outside of Moq and/or RhinoMocks' sweet spot.

The main question you have to ask yourself in choosing is whether you want to keep your testing relatively low-tech or get involved in a higher level of technical trickery. This will largely be dictated by what you're doing - you've tagged ASP.NET and not MVC which suggests that it may indeed be relevant for you.

Have a look at this Dimecasts.net video for a nice overview.

Bagman answered 14/6, 2010 at 7:58 Comment(1)
@bobsmith123: In that case, I personally wouldnt see a reason to use a more heavyweight mocking framework - I'd defer pulling in such artillery until necessary. Using Pex is a separate decision though which should be made independent of your choice of mocking framework. Why did you tag the q with Pex and then not refer to it - are you interested in using it / are you asking are they peanut butter and jelly / do you believe it's indivisible from Moles?Bagman

© 2022 - 2024 — McMap. All rights reserved.