我正在阅读这份文件 sinonjs.org对我来说, stub 和模拟之间的区别不是很清楚。
有人可以用简单的词和一些例子向我解释 stub 和模拟之间的区别吗?
附言:
我已经读过 What is the difference between mocks and stubs ( JMock) ,但答案没有例子。
最佳答案
我将尝试用几句话来解释:
mock:如果您要验证 SUT 中的协作,请使用它。您必须模拟协作者,然后验证协作是否完成。
var collaborator = {}; collaborator.collaboration = sinon.mock(); SUT.setCollaborator(collaborator); SUT.play(); collaborator.collaboration.verify();
stub :如果您需要 SUT 的协作者,请使用它,但测试不是测试协作。
var collaborator = {}; collaborator.collaboration = sinon.stub().returns(1); SUT.setCollaborator(collaborator); SUT.play();
stubs 和 mocks 背后的技术是相似的,区别在于测试的目的。
来自 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.
关于javascript - 在测试 spy 的上下文中, stub 和模拟之间有什么区别? ( Jasmine ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12071192/