Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rhino Mocks - Do we really need stubs? [closed]

If it's possible to change mock behaviour in Rhino Mocks using mock.Stub().Return(), why do we need Stubs anyway?

What do we lose by always using MockRepository.GenerateMock()?

One big benefit of using Mocks instead of Stubs is that we will be able to reuse the same instance among all the tests keeping them cleaner and straightforward.

The moq framework works in a similar way... we don't have different objects for mocks and stubs.

(please, don't answer with a link to Fowler's "Mocks aren't stubs" article)

like image 733
Marcelo Oliveira Avatar asked Sep 10 '12 21:09

Marcelo Oliveira


People also ask

Are mocks stubs?

Stub: Stub is an object that holds predefined data and uses it to answer calls during tests. Such as: an object that needs to grab some data from the database to respond to a method call. Mocks: Mocks are objects that register calls they receive.

What is Rhino Mocks?

What is it doing? Rhino Mocks will generate fake objects to replace the dependencies that you have, and then allow you to tell them, at runtime, how to behave. This functionality is very powerful, and it means that you can tell your fake objects, for each test, how to behave.

What is mock stub fake?

Stub - an object that provides predefined answers to method calls. Mock - an object on which you set expectations. Fake - an object with limited capabilities (for the purposes of testing), e.g. a fake web service. Test Double is the general term for stubs, mocks and fakes.


1 Answers

Keep in mind that Rhino is no longer developed1. Newer frameworks drop this mock-stub difference altogether and introduce single term for their test doubles:

  • Moq: mock
  • FakeItEasy: fake
  • NSubstitute: substitute

Evolution of mocking frameworks seems to push towards "one general purpose entity", instead of having separate different ones depending on test case context.

To learn more on how that separation (mock, stub, fake) originated and what purposes it served, I suggest reading Mark Seemann's article about continuum of test doubles:

At the one extreme you'll find dummies with absolutely no implementation, and at the other end are full production implementations. Dummies and production implementations are both well-defined, but stubs, spies, and fakes are more difficult to pin down: when does a test spy become a fake? In addition, mocks inhabit a rather large interval in the continuum, since they can be quite complex in some instances but very simple in others.


It might seem that Rhino doesn't distinguish between mock and stub, but there are subtle differences. For example, consider stubbing property getter:

var mock = MockRepository.GenerateMock<IService>();
mock.Stub(m => m.Property).Return(42);

This is how you have to do it when object is mock. Stub on the other hand, introduces property semantics, which trivialize entire thing:

var stub = MockRepository.GenerateStub<IService>();
stub.Property = 42;

Even though that's the only one that's coming to my mind at this moment, there might be some more. But still, those are just minor nuances.

1: As of 05/19/2013, this may no longer hold true: Rhino Mocks new home

like image 149
k.m Avatar answered Oct 12 '22 11:10

k.m