【发布时间】:2023-03-12 12:15:01
【问题描述】:
有谁知道链接或参考,我可以在其中找到 axios.put 或 post 的示例 sinon 测试? 目前我正在研究 react js,我想尝试使用 sinon 模拟测试我的 axios.put 或发布。任何帮助将不胜感激。
【问题讨论】:
有谁知道链接或参考,我可以在其中找到 axios.put 或 post 的示例 sinon 测试? 目前我正在研究 react js,我想尝试使用 sinon 模拟测试我的 axios.put 或发布。任何帮助将不胜感激。
【问题讨论】:
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()
})
})
【讨论】: