如何使用Chai / Sinon

时间:2015-12-10 11:51:01

标签: javascript jasmine bdd sinon chai

我在Jasmine中有以下代码, add 是间谍。

expect(add).toHaveBeenCalledWith('MY_OBJECT_ID', jasmine.any(Object));

我如何用Chai / Sinon表达这个?我知道Sinon会使用to.have.been.called.with()但是我遇到问题的部分是jasmine.any()函数。

2 个答案:

答案 0 :(得分:1)

我做了一些挖掘并想出了这个....

expect( add.lastCall.args[0] ).to.equal('MY_OBJECT_ID');
expect( add.lastCall.args[1] ).to.be.an('object');

答案 1 :(得分:1)

Sinon有calledWithMatch()

var sinon = require('sinon');
var spy   = sinon.spy();

spy('MY_OBJECT_ID', { foo : 'bar' });

console.log(spy.calledWithMatch('MY_OBJECT_ID',     sinon.match.object) ); // true
console.log(spy.calledWithMatch('NOT_MY_OBJECT_ID', sinon.match.object) ); // false
console.log(spy.calledWithMatch('MY_OBJECT_ID',     sinon.match.number) ); // false