Search code examples
c#unit-testingmockingstandardsisolation-frameworks

Standards for Mock Objects


In Roy Osherove's book [Unit Testing][1] book, he explains that a single unit test should contain between 0 and 1 mocks. He suggests that if your test isn't asserting on the mock, then don't use a mock at all. He further demonstrates how to use an isolation framework to produce stubs, which were created similarly to the mocks. He places no suggested limit on the number of stubs created per test.

My question is this: can those recommendations be applied to all isolation frameworks (or all popular C# frameworks)? In other words, is there a framework that can generate only mocks - not stubs? Is there an isolation framework that does not distinguish mocks from stubs?

I'm just curious how easy Osherove's recommendations can be converted into coding standards.

[1]: http://the system under test isn’t even being tested at all, instead data returned from mocks is what is being tested.


Solution

  • There are frameworks like Moq that does not distinguish them. FakeItEasy goes even further and calls everything fake objects.

    Yes, they can be applied, because mocks are just smarter stubs. It does not matter that much if stub gets called as mock as long as You don't assert on more than one mock. That recommendation in other words is more about asserting only one thing per test. Explicitly distinguishing mocks and stubs isn't that important.