javascript - 在测试 spy 的上下文中, stub 和模拟之间有什么区别? ( Jasmine )

标签 javascript jasmine

我正在阅读这份文件 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/

相关文章:

javascript - Firefox 中的 E2E Protractor 测试显示有 8 个待定规范这是什么意思?

javascript - 如何在 .hbs 文件中执行 sql 查询,就像我们使用 php 执行 for .html 一样?

javascript - 从 iframe 内部访问父内容(在同一台服务器机器上)jquery 在 firefox 和 IE 中工作但在 chrome 中不工作

javascript - 在 forEach() 中使用 `this`

javascript - 为什么我在 Jasmine 1.3.x 中的 AngularJS 异步测试不起作用?

javascript - 如何在javascript文件之间共享数据?

gruntjs - Karma 从 0.12 升级到 0.13 后测试失败

javascript - HTML5 验证的表单事件失败?

javascript - 难以使用 Javascript 切换媒体查询

javascript - 睾丸错误: Cannot start chrome