views:

677

answers:

5

I'm new to mocking, I have a new .net web project that is in UI->BLL->DAL->DB structure, I use NUnit to do some testing currently. I intent to use it to test middle tier so I don't have to actually write to DB.

Now, I've never done any mocking, don't quite know where to start, so I am looking for a mocking framework that has some end to end samples! Could someone point to me a some mocking material that starts from the beginning and with full samples please?

Thank you,

Ray.

+3  A: 

Rhino Mocks is one framework you could use that I have used a little bit.

Cheat sheet has a few links if you want to try out some others though the article is a bit dated.

JB King
I second rhino mocks I picked it up in an hour.
Omar Kooheji
+2  A: 

TypeMock?...

well, best mocking for me - interfaces, some refactoring of your code a bit and figuring out what do you want to test so that you don't need any mocking. Or - any dependency injection to mock out some stuff (but before you need to refactor a bit of course)

badbadboy
+6  A: 

You should check out some videos about mocking on Dimecasts.net, it's a quick way to get a feel over what mocking is about and get started on your own code.

Introduction to Moq

Introduction to RhinoMocks

rodbv
Those are very nice indeed, learned a lot from those short clips :).
Morph
+3  A: 

At the moment there are a number of different mocking frameworks. I would recommend that you either take a look at RhinoMock or TypeMock. Both are free for personal / open source projects. TypeMock has a corporate license as well.

RhinoMock forces you to refactor your code for testability (if needed, if you already have testable code you're doing good). This requires more work, but it will leave you with code which is loosely coupled which is a boon in itself. Due to this there are certain constructs you simply can't mock directly with Rhino. However, you can always introduce additional layers of indirection and solve it that way. The bottomline however is this: you need to do some more work, but ultimately the refactoring will benefit your code.

TypeMock on the other hand works by modifying the code on-the-fly (it uses the profiler API to inject code). This allows you to employ mocking for code that isn't otherwise suitable for this type of testing. TypeMock will pretty much allow you to mock anything (except for mscorlib), so it is easy to get started and it works well with legacy code. However, because you're not forced to refactor your code, you don't get the additional benefit of loosely coupled types. Additionally TypeMock will sometimes lead to very weird errors due to the fact that the running code is modified.

Brian Rasmussen
A: 

You might be interested in our book in progress http://www.mockobjects.com/book. It's Java, but the principles are the same.

Steve Freeman