0

So far I've been using NUnit.Mocks to isolate my classes but I am getting annoyed by the lack of feedback it gives me. So I've been looking around for alternatives but getting nowhere.

Rhino Mocks: Can't figure it out. Everything has to be done in a totally different obscure manner making the test code practically unreadable.

Moq: Can't figure out how to mock property getters. There are samples but they don't work when I try them.

From what I've gathered these seem to be among the most popular ones. Before I go and try every single one out there I'd like to ask you folks for suggestions...

What I am looking for:

  • Simple design. I don't want to memorize 100 different statements and which one to use depending on whether my method has a return value or not, whether my property has both getters and setters or just one of them, whether it's a full moon or not, etc...
  • Elaborate feedback. If my test fails I want to know what method call was missing or too much. I want to know which call had the wrong arguments. Etc...

My background is C# 2.0. I'm still pretty UN-familiar with the concepts in newer .NET versions. So a framework that doesn't require those things would be a bonus.

Thank you.

Edit: I finally figured out why my Moq tests didn't work. It had nothing to do with Moq and I am now evaluating it further. Looks very good so far...

Wooble
  • 87,717
  • 12
  • 108
  • 131
Kempeth
  • 1,856
  • 2
  • 22
  • 37
  • How is this not going to turn into a subjective/argumentative "my favorite is best?" You've eliminated the two most powerful and popular mocking frameworks. What's left? (Typemock :) – Dave Swersky Mar 02 '11 at 14:33
  • PS- the most powerful features of modern mocking frameworks will include lambdas, Func, and generics to support fluent interfaces. I don't think you'll be able to stick with .NET 2.0 and find a framework that doesn't use those features. – Dave Swersky Mar 02 '11 at 14:40
  • NUnit.Mock is not meant for public use - it is used internally by NUint when developing and is not supported. According to their documentation! Moq is all kinds of awesome. – Peter Kelly Mar 02 '11 at 14:41
  • @Swersky: No I'm not looking for everyone's favorite. I'm looking for suggestions to solve the problems I've been having with the frameworks I've tried so far. | As I said, a C# 2.0 level would be a bonus - It is not a requirement... – Kempeth Mar 02 '11 at 14:57
  • I can understand the downvote, but this seems like a pretty good question to me, since you're asking for suggestions rather than "which one is best?" It's also a great topic, IMHO, so +1 from me. – Justin Morgan - On strike Mar 02 '11 at 16:35

2 Answers2

5

I use Moq and I prefer the implementation rather than the Replay method of some other mocking frameworks - I found it more natural.

What problem are you having with Properties? To mock returning a value from a property it is just

mock.Setup(foo => foo.Name).Returns("bar")

Simple Design

Moq has a fluent interface and the code can be expressive and quite beautiful really. For example:

  1. Injecting mocks into object

    mockView = new Mock<IView>();
    mockModel = new Mock<IModel>();
    realPresenter = new Presenter(mockView.Object, mockModel.Object);
    
  2. Testing behavior of Presenter reacting to View event

    mockView.Raise(v => v.SomeEvent += null, EventArgs.Empty);
    mockModel.Verify(m => m.DoSomething());
    

Elaborate Feedback

I find the failure feedback very useful and I can usually find the problem very quickly. Say you expect a method with certain parameter value to be called and it fails.

Test 'MyProject.MyTest' failed: Moq.MockException : Expected invocation on the mock at least once, but was never performed: v => >v.DoSomething("Something")> No setups configured.

Performed invocations: View.DoSomething("SomethingElse") at Moq.Mock.ThrowVerifyException(MethodCall expected, IEnumerable1 setups, IEnumerable1 actualCalls, Expression expression, Times times, Int32 callCount) at Moq.Mock.VerifyCalls(Interceptor targetInterceptor, MethodCall expected, Expression expression, Times times) at Moq.Mock.Verify[T](Mock mock, Expression1 expression, Times times, String failMessage) at Moq.Mock1.Verify(Expression`1 expression) MyTest.cs(118,0):

The Moq Quickstart is very well put together and has been the only reference I have needed so far.

Peter Kelly
  • 14,253
  • 6
  • 54
  • 63
2

FakeItEasy was designed with simplicity (discoverability) and also very importantly feedback as the two main design goals from the ground up.

Another main design goal was readability of the tests.

You say you don't want to remember 100 different statements? Well, good, FakeItEasy uses no more than you can count on the fingers on one of your hands.

For example, configuration and assertion is done through the same statement:

// Creating a fake
var foo = A.Fake<IFoo>();

// Configuration
A.CallTo(() => foo.Bar()).Returns("a value");

// Assertion
A.CallTo(() => foo.Bar()).MustHaveHappened();
Patrik Hägne
  • 16,751
  • 5
  • 52
  • 60
  • As a side not, this is obviously not C# 2.0 level since it uses a lambda expression. However it removes much of the "toughness" of lambda expressions that other frameworks suffers from since no argument is passed to the lambda. You learn to remember to type () => after you've done it two times. – Patrik Hägne Mar 02 '11 at 22:05
  • I've looked into it. Unfortunately all current versions require .NET 4.0 and I only have Visual Studio 2008. – Kempeth Mar 03 '11 at 07:24
  • Version 1.0.0.5 does work with .Net 3.5. It's an older release so it does not contain all new features but it's still maintained and bug fixes will be made to it. – Patrik Hägne Mar 03 '11 at 10:11
  • Yeah I know. But I don't like to use a framework that's no longer developed. Not if I have other good options... But still, thanks! – Kempeth Mar 04 '11 at 10:09
  • 3
    .Net 3.5 is a framework that's no longer developed. – Patrik Hägne Mar 04 '11 at 16:24
  • 1
    Yeah. But which .NET framework I use is not my choice... – Kempeth Mar 11 '11 at 12:20