小编Smu*_*Web的帖子

如何使用sinon/chai测试axios请求参数

我试图使用sinon/chai/mocha来测试axios调用的参数,以确认某些参数的存在(理想情况下它们是有效的日期).

示例代码(在类myclass中)

fetch() {
  axios.get('/test', { params: { start: '2018-01-01', end: '2018-01-30' } })
  .then(...);
}
Run Code Online (Sandbox Code Playgroud)

示例测试

describe('#testcase', () => {
  let spy;
  beforeEach(() => {
    spy = sinon.spy(axios, "get");
  });
  afterEach(() => {
    spy.restore();
  });
  it('test fetch', () => {
    myclass.fetch();
    expect(spy).to.have.been.calledWith('start', '2018-01-01');
    expect(spy).to.have.been.calledWith('end', '2018-01-30');
  });
});
Run Code Online (Sandbox Code Playgroud)

不过,我已经尝试了许多选择,包括的匹配,expect(axios.get)... expect(..).satisfy,getCall(0).args和爱可信-模拟适配器,但我无法弄清楚如何做到这一点.我错过了什么?

javascript sinon chai sinon-chai

5
推荐指数
1
解决办法
983
查看次数

标签 统计

chai ×1

javascript ×1

sinon ×1

sinon-chai ×1