2017-03-15 48 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 
相關問題