Reputation: 1995
I have a request where I am using Axios API to make external request.
Example.
axios({
method: 'get',
url: 'xxxxx',
responseType: 'json'
})
.then((data) => data)
.catch((err) => err)
How we can stub API response.
I have tried stubbing methods request
, options
.
How can I stub above API call.
sandbox.stub(externalRequest.Axios.prototype, 'init').resolves(resp);
Thank you.
Upvotes: 0
Views: 1196
Reputation: 35553
If you need to stub axios
for your tests, I recommend you to check out axios-mock-adapter which allows you to mock responses with an easy API.
var axios = require("axios");
var MockAdapter = require("axios-mock-adapter");
// This sets the mock adapter on the default instance
var mock = new MockAdapter(axios);
// Mock any GET request to /users
// arguments for reply are (status, data, headers)
mock.onGet("/users").reply(200, {
users: [{ id: 1, name: "John Smith" }],
});
Upvotes: 1