最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - to.have.been.calledWith is not a function error in chai#3.5.0 - Stack Overflow

programmeradmin8浏览0评论

I've updated the version of chai in my project and after updating it to 3.5.0, some tests are failing. I see that I'm not able to test arguments of the function for which I did spy.

I created a fiddle to reproduce my issue with sample method here - 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);
  });
});

Can anyone suggest how can we test argument in newer version chai.js?

I've updated the version of chai in my project and after updating it to 3.5.0, some tests are failing. I see that I'm not able to test arguments of the function for which I did spy.

I created a fiddle to reproduce my issue with sample method here - 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);
  });
});

Can anyone suggest how can we test argument in newer version chai.js?

Share Improve this question asked May 16, 2017 at 8:11 Jay ShuklaJay Shukla 5,8268 gold badges34 silver badges65 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 13

As you are using Sinon, you can either use the Sinon spy methods and check the results with Chai:

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

Or you can use sinon-chai which will let you do:

expect(spy).to.have.been.calledWith(5);

See a JSFiddle of the first example

Maybe you forgot to include the sinon-chai.js? Here is the working fiddle https://jsfiddle/zjet0432/1/ I only added the sinon-chai.js and change your last line to expect(spy).to.have.been.calledWith(5);

If you are using TypeScript with Sinon and Chai and want to use nice calledWith assertions in Chai, use sinon-chai imports like this:

import chai, {expect} from 'chai'
import sinon from 'ts-sinon'
import sinonChai from 'sinon-chai' // <--- NOTE

then configure chai with:

chai.use(sinonChai)

and then use assertions like:

let send = sinon.spy()
...
expect(send).to.have.been.calledWithExactly('Greetings!')

Runnable example here (disclaimer: my own repo)

发布评论

评论列表(0)

  1. 暂无评论