有谁知道我可以在其中找到 axios.put 或 post 的示例 sinon 测试的链接或引用吗? 目前我正在研究 React js,我想尝试使用 sinon 模拟来测试我的 axios.put 或 post 。任何帮助将不胜感激。
最佳答案
const MyService = {
save: body => axios.post('/save', body).then(response => Promise.resolve(response.data))
}
describe('MyService save', () => {
it('should parse and return the response data', () => {
const stubBody = { nic: 'cage' }
const stubResponse = {status: 200, statusText: 'OK', data: { oscars: 1 } }
const stubPost = sinon.stub(axios, 'post').withArgs('/save', stubBody).returns(Promise.resolve(stubResponse))
expect(MyService.save(stubBody)).to.eventually.satisfy(_ => _.oscars === 1)
stubPost.restore()
})
})
关于reactjs - 使用 sinon 进行 axios.put 或 post,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45236019/