Search code examples
c#.netunit-testingtddrhino-mocks

If I am able to call AssertWasCalled method on a Stub, what is the difference between mock and stub?


As far as I know, Stub is just a replacement for a dependency. Stub is not used for verificiation and it cannot fail a test -from the book The Art Of Unit Testing.

But it seems since Rhino Mocks 3.5 I can do this:

var service = MockRepository.GenerateStub<ILuckyService>(); 
service.AssertWasCalled(s=>s.GetLuckyNumberOfTheDay());

If can verify whether a method is called on a stub, then why do I need to bother myself if I need to use a stub or mock?


Solution

  • Use Martin Fowler's article as the shared reference within your team. The distinction is important for the readers of your test : When I see a stub, I ignore it and move on.

    • A stub is just there to make your test work (NOP methods / return some canned value). It is not the focus of the test, rather an incidental detail. (Albeit a mandatory detail/dependency for your test scenario)
    • A mock on the other hand is your primary focus... you want to test whether your action resulted in the specific expectation being met on the (mocked) dependency.

    That's my take on it. Even if Rhino Mocks allows you to do this, I wouldn't assert on a Stub.