简单地期望一次参数

时间:2016-11-15 14:15:34

标签: mocha sinon chai

我试图让这个简单的测试工作。

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方法。它存在吗?

1 个答案:

答案 0 :(得分:0)

您可以使用 args 来验证发送给该功能的参数。

 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.

这是验证args的一种方法。

相关问题