3

I've been thinking about learning about stubs and mocks, and I know of several of the available libraries for .Net: RhinoMocks, Moq, TypeMock... and although I still don't have too clear the difference between stubs, and mocks, I know it is there, and I could always google about it.

I'd like to know, without being into TDD or anything similar:

  • Is the learning curve worth it? I'm not so sure learning all the minor details about expectations or whatever would pay back in this situation.

  • Is the production gain so bigger than manually making my "dummy implementations"? I guess so, since mocking/stubbing a class dynamically is faster.

  • Which library would you recommend? Moq seems the easier one.

  • Which are the best resources (tutorials, books, videos...) for dummies and more or less up to date? I wouldn't like to learn about a library, and when I download the last release, realize that most of the API shown is no longer valid.

Thanks for everything.

Neverbirth
  • 1,044
  • 8
  • 20
  • 1
    As answers are options, this question should be Community Wiki (IMHO) – Binary Worrier Nov 12 '10 at 10:20
  • 1
    Using the right tools, the learning curve can be very easy. E.g., in java using mockito is so easy, it takes about 10 minutes to learn. I'm sure you'll find a similar library for .Net (or several). BTW, this questions seems like a dup - I'm sure it has been asked before. Try searching SO a bit. – ripper234 Nov 12 '10 at 10:22
  • I don't have enough privileges to start wiki posts. About searching SO, well, I searched a bit, and saw a couple other posts, but they were either old, or didn't cover my questions at the level I wanted, so thought it was not a bad idea to start a new question. – Neverbirth Nov 12 '10 at 10:29

4 Answers4

5
  • Learning curve worth it? - Yes, it's not that hard. Once you know how to use a mocking framework you'll save a load of time in unit testing.

  • Better than manual mocks? - Not necessarily, in some situations manual mocking can be better. But IMO, mocking frameworks are usually the fastest way to get things done, with the least code. Try both approaches and you'll get to know which is best in a given situation.

  • Which library? - Moq and Rhino Mocks are the most popular for .Net according to this poll, and both have plenty features and are easy to use. With Rhino there are two ways of mocking ('record & playback' and 'AAA' mode) - I'd only use the AAA mode. I think Moq only works like Rhino 'AAA' mode, so that might be slightly less confusing.

  • Not sure about specific resources, just pick a tool and google it, you'll find plenty.

In terms of the difference between mocks & stubs, a mock can record what happened to it (e.g. what methods were called on it), whereas a stub can't. You'll find out more when you start using them.

Grant Crofton
  • 8,895
  • 5
  • 28
  • 38
  • I expected NMock and JustMock to have some more votes. I saw the record and playback call of Rhino in some samples, and got a bit confused about how the complexity it adds is worth it, anyway, I don't actually know how they work/what they actually do, so I cannot comment on it at all. – Neverbirth Nov 12 '10 at 10:39
  • Yeah, the playback thing is the old way of doing it (and doesn't allow you to follow the 'Arrange Act Assert' pattern in your unit tests - aka AAA), personally I think you can forget about it. That's why I think Moq might be a better choice, as it doesn't have the legacy of those API calls like Rhino does, so it should be a bit easier to learn. – Grant Crofton Nov 12 '10 at 10:49
2

I've been using FakeItEasy for a couple of months now, and it is Faking Amazing!
I can't recommend it highly enough.

Binary Worrier
  • 50,774
  • 20
  • 136
  • 184
  • Noted up, I didn't know about it, although it's been around a month since last source commit, is it still active? Guess so. – Neverbirth Nov 12 '10 at 10:31
  • It's still active, I haven't pushed for a while though. There are no feature requests at the moment so I have local commits only. – Patrik Hägne Nov 13 '10 at 20:23
0

I actually use RhinoMocks, and while the learning curve is quite painful, now I'm very happy with the results. Anyway, it takes (or, at least, it took me but maybe I'm a little dumb :)) some time before you get faster than manually making dummy implementations. I never used any of the other libraries you mention, so I can't make a comparison between them.

The homepage contains also some good documentation.

Overall, I think it was a worthwhile effort and I'd recommend it.

Simone
  • 11,655
  • 1
  • 30
  • 43
  • 1
    One hurdle I saw with Rhino Mocks is that its official documentation isn't very nice, is it? Rhino Mocks was my first bet when decided to learn more about it. – Neverbirth Nov 12 '10 at 10:40
  • Yes, you're totally right: it's the *biggest* hurdle. Anyway, you can find much info scattered through the internet, i.e. right here on stackoverflow you can find lots of info. – Simone Nov 12 '10 at 10:48
  • That's why I asked about the best resources for learning, and why I talk about the official documentation, when learning something new and that appears to be a wide subject, I like centralized resources instead of having to look through several sites with information that may end being outdated, scarce or not following my current knowledge level. – Neverbirth Nov 12 '10 at 11:46
  • Well, it depends on what's your preferred approach to learn using new things. If you, like me, rather look at complete commented examples then the official documentation is not that good. Otherwise, if you have a more "academic" approach the official documentation could be good enough. Hope I have explained myself :) – Simone Nov 12 '10 at 13:21
0

Stub - fake object that mimics behavior of "real" object and is responsible of keeping test runnable.

Mock - stub that is used to assert if test passes.


About mocking frameworks - this question has been asked before.

My own preference is to use both approaches. When I feel using framework easier I go with that and vica versa - sometimes simple fakes are enough and easier to understand. Unfortunately - You won't be able to judge by yourself until You actually learn using one.

Moq lately seems to be most trending mocking framework at .Net space. I'm using it too.

Community
  • 1
  • 1
Arnis Lapsa
  • 45,880
  • 29
  • 115
  • 195