【发布时间】:2016-10-24 18:55:36
【问题描述】:
我稍作改动以隐藏我无法分享的细节,但我正在测试的示例函数(我们称之为 checkbro.js)在这里...
checkbro.js
function saveRecord(context) {
if (!checkIfBro.knowsABro(context.currentRecord)) {
if (confirm('Do you Bro?')) {
context.currentRecord.setValue('bro', -10, true);
} else {
alert('Please someone has to be a bro');
}
}
return true;
}
我的一个测试示例(它失败了 ReferenceError: confirm is not defined)...
checkbro.spec.js
it('should only execute for create new bro master', function() {
let checkIfBro = { knowsABro: sinon.spy() };
let confirm = {confirm: sinon.spy()};
let record = {currentRecord: { setValue: sinon.spy()}};
let checkIfBroRec = requirejs('contacts/checkbro', [checkIfBro, log]);
checkIfBroRec.saveRecord({ record: record });
checkIfBroRec.knowsABro.called.should.be.true;
record.currentRecord.setValue.called.should.be.true;
});
一点上下文,我已经完成了很多浏览器测试并且刚刚进入单元测试,所以这里有一些学习曲线。任何更好地使用 sinon 或其他模块我都在听。这也是开发者写的,现在我接手了。
【问题讨论】:
-
你在哪里将confirm spy传递给函数?如果你使用
requirejs('contacts/checkbro', [checkIfBro, log]);这一行传递依赖项,那么你应该包含confirm spy,所以函数会有一个引用。跨度>
标签: javascript unit-testing requirejs mocha.js sinon