2016-11-15 23 views
0

我試圖讓這個簡單的測試工作。簡單期望一次參數

let querySpy = sinon.spy(db.query); 

querySpy.expects().once().withArgs(`USE myDatabase`); 

我試圖把一個間諜放在一個方法,期望它被調用一次具體的參數。說起來容易做起來難。

我不能把間諜放在db,結果在TypeError: Attempted to wrap undefined property undefined as function(這是一個對象)。

我看:https://gist.github.com/yoavniran/1e3b0162e1545055429e#sinon-chai

我無法找到一個calledOnceWith方法。它存在嗎?

回答

0

您可以使用參數來驗證發送給函數的參數。

sinon.spy(db, "query"); //spy on existing method  
expect(db.query.calledOnce).to.be.true; //verify is called once 
const args = db.query.getCalls()[0].args; //get the arguments 
expect(args[0]).to.equal(`USE myDatabase`); //verify the arguments. 

這是驗證參數的一種方法。