如何使用Chai/Sinon重构jasmine.any()函数

How do I refactor the jasmine.any() functionality using Chai / Sinon

本文关键字:any 函数 jasmine 重构 何使用 Chai Sinon      更新时间:2023-09-26

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

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

我该如何用Chai/Sinon来表达这一点?我知道Sinon过去会被调用.with(),但我遇到的问题是jasmine.any()函数。

我做了一些挖掘,得出了这个。。。。

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

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