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?
console.log(_spy.args)
just aftermyclass.fetch()
in the testcode? – Interrogate_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!) – Ailyn