2017-03-15 50 views
0

比方说我们这是导出函数如何用Mocha,Chai和Sinon检查函数被调用的参数的数量?

function bar(x,y){ 
    console.log(x,y); 
} 

服务foo和我们想要写一个单元测试,将测试这个函数被调用以2个参数。 我已经试过这

var args = sandboxSinon.spy(Foo, 'bar').getCalls()[0].args; 

,这是返回

undefined is not an object (evaluating 'sandboxSinon.spy(Foo, 'bar').getCalls()[0].args

有人能搞清楚发生了什么或者我怎么能测试吗?

回答

1

下面是一个例子:

const sinon = require('sinon'); 

const Foo = { 
    bar(x,y) { 
    console.log(x, y); 
    } 
}; 

let spy = sinon.spy(Foo, 'bar'); 

Foo.bar('hello', 'world'); 

console.log(spy.firstCall.args.length); // => 2 
相关问题