我已经在项目中更新了chai的版本,并将其更新为3.5.0后,某些测试失败了。我看到我无法测试我所监视的函数的参数。

我创建了一个 fiddle 来在这里用示例方法重现我的问题-JSFiddle

describe('Mocha + Chai JsFiddle', function() {

  it('should test arg', function() {
    var spy = sinon.spy(test, 'testFun');

    test.testFun(5);

    expect(spy).to.have.been.called.with(5);
  });
});

谁能建议我们如何在较新版本的chai.js中测试参数?

最佳答案

使用Sinon时,可以使用Sinon spy 方法并使用Chai检查结果:

expect(spy.calledWith(5)).to.equal(true);

或者,您可以使用sinon-chai来执行以下操作:
expect(spy).to.have.been.calledWith(5);

See a JSFiddle of the first example

10-04 22:47
查看更多