149
votes

I haven't play enough with this and usually use mocks, but I wonder what are the differences between this two and when to use one or the other on Rhino Mocks.

Update:

I also found the answer to my question in Ayende's words:

The difference between stubs and mocks

You can get the actual definition of the these terms in this article: Mocks Aren't Stubs. I want to focus on the difference from the point of view of Rhino Mocks.

A mock is an object that we can set expectations on, and which will verify that the expected actions have indeed occurred. A stub is an object that you use in order to pass to the code under test. You can setup expectations on it, so it would act in certain ways, but those expectations will never be verified. A stub's properties will automatically behave like normal properties, and you can't set expectations on them.

If you want to verify the behavior of the code under test, you will use a mock with the appropriate expectation, and verify that. If you want just to pass a value that may need to act in a certain way, but isn't the focus of this test, you will use a stub.

IMPORTANT: A stub will never cause a test to fail.

5

5 Answers

149
votes

As per this

... Put simply there is a difference between Mock and Stub objects and RhinoMocks recognizes that allowing us to write tests that better state their purpose.

Mock objects are used to define expectations i.e: In this scenario I expect method A() to be called with such and such parameters. Mocks record and verify such expectations.

Stubs, on the other hand have a different purpose: they do not record or verify expectations, but rather allow us to “replace” the behavior, state of the “fake”object in order to utilize a test scenario ...

20
votes

Generally Speaking, Unit tests call functions and methods, and then check to see if the expected behavior took place. These functions and methods might require parameters. We use stubs and mocks to satisfy these parameters. We might sometimes also mock global objects.

Stubs

A Stub is a tiny fake object that your test can use as a parameter to make the function call work. This lets us verify the behaviour of the function under test. It doesn't let us verify any side effects, because the stub has no implementation.

Mocks

A Mock is a stub with an implementation. If our function under test interacts with our mock object, we can verify that the mock has been interacted with as we expected.

For example, say we had a mock User object, and we wanted to verify that our session.login method worked, we might want to check that user.lastLoggedIn was set. We could create a mock User that implements this method. When we call session.login, we can assert that user.lastLoggedIn has the state we expected.

To sum up

A mock is a stub with an implementation, which lets us test side effects.

Is this difference still important?

Rather like the difference between similes and metaphors, the difference between stubs and mocks is subtle and historical, and perhaps has more to do with the different communities and philosophies in the testing world than any major technical difference.

They represent slightly different approaches to testing. A mock can be written like a stub. A stub can usually be expanded into a mock.

Which should you use?

You may find that you start out creating stubs, then later you may find that you need to create full on mocks for some of your objects. You might want to mock everything as you go, or you might just want to mock where required.

7
votes

Difference between Mock and stub: with stub, you fix the input of your unit test: so your unit test doesn't make assertion on stub and Stub by rewriting the implementation of some method fix the behavior of fake object. with Mock, you fix the ouput of your unit test: so your unit test make an expectation on your Mocking object by checking internal interaction in your mock object.

5
votes

In case of Moq framework - setup method is STUB where as Verify method is Mock

0
votes

One thing that I noticed too is that when I use MockRepository.GenerateMock, I need to explicitly set expectations on a specific method call to intercept that call. With stubs, it seems to automatically intercept any method as long as it is virtual.