What are the differences between mocks and stubs on Rhino Mocks?

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.


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 ...