javascript - How to test axios requests parameters with sinonchai - Stack Overflow

I am trying to test the parameters of an axios call using sinonchaimocha, to confirm the existenc

I am trying to test the parameters of an axios call using sinon / chai / mocha, to confirm the existence of certain parameters (and ideally that they are valid dates with moment).

Example code (in class myclass)

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

Example test

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');
  });
});

However, I have tried many options including matchers, expect(axios.get)... expect(..).satisfy, getCall(0).args and axios-mock-adapter, but I cannot figure out how to do this. What am I missing please?

I am trying to test the parameters of an axios call using sinon / chai / mocha, to confirm the existence of certain parameters (and ideally that they are valid dates with moment).

Example code (in class myclass)

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

Example test

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');
  });
});

However, I have tried many options including matchers, expect(axios.get)... expect(..).satisfy, getCall(0).args and axios-mock-adapter, but I cannot figure out how to do this. What am I missing please?

Share Improve this question edited Nov 20, 2019 at 5:33 Lin Du 103k136 gold badges334 silver badges567 bronze badges asked Jun 11, 2018 at 15:27 SmurfTheWebSmurfTheWeb 932 silver badges11 bronze badges 6
  • sorry, example was bad as it did not call the function! I have updated it now - it is not the exact code as I cannot share that. – SmurfTheWeb Commented Jun 11, 2018 at 15:37
  • Could you let me know the console.log(_spy.args) just after myclass.fetch() in the testcode? – n8o Commented Jun 11, 2018 at 15:46
  • Have you tried passing it other parameters within your test and them expect them to beEqual? That's the only way I can imagine right now that you would test that – mblancodev Commented Jun 11, 2018 at 16:04
  • @YonggooNoh console.log(_spy.args) produces [] – SmurfTheWeb Commented Jun 11, 2018 at 20:21
  • 1 Turns out, the function I was testing was changed and so was not passing in the parameters.. so in this case, the test was (correctly) failing! I can confirm when using the above framework, _spy.args contains [ [ '/test', { params: { start: '2018-01-01', end: '2018-01-30' } } }. Using _spy.args[0][1].params can access it (though I am sure there is a better way!) – SmurfTheWeb Commented Jun 11, 2018 at 20:40
 |  Show 1 more ment

1 Answer 1

Reset to default 4

Here is the unit test solution, you should use sinon.stub, not sinon.spy. Use sinon.spy will call the original method which means axios.get will send a real HTTP request.

E.g. index.ts:

import axios from "axios";

export class MyClass {
  fetch() {
    return axios.get("/test", {
      params: { start: "2018-01-01", end: "2018-01-30" }
    });
  }
}

index.spec.ts:

import { MyClass } from "./";
import sinon from "sinon";
import axios from "axios";
import { expect } from "chai";

describe("MyClass", () => {
  describe("#fetch", () => {
    let stub;
    beforeEach(() => {
      stub = sinon.stub(axios, "get");
    });
    afterEach(() => {
      stub.restore();
    });
    it("should send request with correct parameters", () => {
      const myclass = new MyClass();
      myclass.fetch();
      expect(
        stub.calledWith("/test", {
          params: { start: "2018-01-01", end: "2018-01-30" }
        })
      ).to.be.true;
    });
  });
});

Unit test result with 100% coverage:

 MyClass
    #fetch
      ✓ should send request with correct parameters


  1 passing (8ms)

---------------|----------|----------|----------|----------|-------------------|
File           |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
---------------|----------|----------|----------|----------|-------------------|
All files      |      100 |      100 |      100 |      100 |                   |
 index.spec.ts |      100 |      100 |      100 |      100 |                   |
 index.ts      |      100 |      100 |      100 |      100 |                   |
---------------|----------|----------|----------|----------|-------------------|

Source code: https://github./mrdulin/mocha-chai-sinon-codelab/tree/master/src/stackoverflow/50801243

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744743571a4591173.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信