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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With