admin管理员组文章数量:1391956
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?
- 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 aftermyclass.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
1 Answer
Reset to default 4Here 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
本文标签: javascriptHow to test axios requests parameters with sinonchaiStack Overflow
版权声明:本文标题:javascript - How to test axios requests parameters with sinonchai - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744743570a2622754.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论