小编jer*_*ans的帖子

如果间接调用间谍方法,则不调用Sinon Spy

问题

在我们的代码库中,我们遇到了sinon的问题,可以使用下面的代码复制.事情是,它似乎是间接称为间谍回归力false,console.log明确表示该方法被称为但spy.called遗骸false.

以下CDN可用于html:

//cdnjs.cloudflare.com/ajax/libs/sinon.js/1.7.3/sinon-min.js
//cdnjs.cloudflare.com/ajax/libs/require.js/2.1.14/require.min.js
Run Code Online (Sandbox Code Playgroud)

main.js

require(['myModule'], function(module) {

    //using sinon
    var methodCallerSpy = sinon.spy(module, 'methodCaller')
    console.log(methodCallerSpy); // methodCaller
    module.methodCaller();
    console.log(methodCallerSpy.called); //true


    var methodSpy = sinon.spy(module, 'method');
    console.log(methodSpy); //method
    module.methodCaller();
    console.log(methodSpy.called); // false
    module.method();
    console.log(methodSpy.called); // true

});
Run Code Online (Sandbox Code Playgroud)

和模块

define(function() {
    const method = () => console.log('method called by methodCaller');

    const methodCaller = () => method();

    return{
        method,
        methodCaller
    }
});
Run Code Online (Sandbox Code Playgroud)

javascript sinon

10
推荐指数
1
解决办法
3132
查看次数

标签 统计

javascript ×1

sinon ×1