Reputation: 2083
I was reading this document sinonjs.org and to me is not clear the different between stubs and mocks.
Could someone explain me with simple words and some example the difference between stubs and mocks?
P.S.:
I already read about What is the difference between mocks and stubs ( JMock), but the answers have no examples.
Upvotes: 4
Views: 2033
Reputation: 21
Below important notes will help you to understand the exact difference between Spies, Stubs and Mocks:
Upvotes: 2
Reputation: 109
I will try to explain in a few words:
mock: use it if you are going to verify a collaboration in your SUT. You have to mock the collaborator and then verify if the collaboration is done.
var collaborator = {};
collaborator.collaboration = sinon.mock();
SUT.setCollaborator(collaborator);
SUT.play();
collaborator.collaboration.verify();
stub: use it if you need a collaborator fot your SUT, but the test is not testing the collaboration.
var collaborator = {};
collaborator.collaboration = sinon.stub().returns(1);
SUT.setCollaborator(collaborator);
SUT.play();
The technology underneath stubs and mocks is similar, the difference is the intention of the test.
From http://sinonjs.org/docs/#mocks:
Mocks come with built-in expectations that may fail your test. Thus, they enforce implementation details. The rule of thumb is: if you wouldn't add an assertion for some call specific, don't mock it. Use a stub instead.
Upvotes: 2