在此 sinon.spy mocha 示例中是否有更改确认和警报的简单方法

Is there an easy way of altering confirm and alerts in this sinon.spy mocha example

我稍作改动以隐藏我无法分享的细节,但我正在测试的示例函数(我们称之为 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 或其他模块我都洗耳恭听。这也是开发者写的,现在我接手了。

It fails ReferenceError: confirm is not defined

在上面的代码中 if (confirm('Do you Bro?')) 使用 let confirm = {confirm: sinon.spy()} 作为函数。这应该抛出 TypeError: confirm is not a function:

var confirm = {};
function foo () {
    if(confirm()){}
}

foo(); // TypeError: confirm is not a function

所以可以肯定的是,不是checkIfBroRec.saveRecord({ record: record })导致代码达到if (confirm('Do you Bro?')),因此问题一定出在:

let checkIfBroRec = requirejs('contacts/checkbro', [checkIfBro, log]);

不幸的是,这部分代码没有完全显示在上面的代码片段中,因此我无法评估导致问题的原因。