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



1 Answers

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.

Question

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.




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




Related