使用sinon进行axios.put或post

时间:2017-07-21 11:02:32

标签: reactjs sinon

有没有人知道我在哪里可以找到axios.put或post的示例sinon测试的链接或参考? 目前我正在研究反应js,我想尝试测试我的axios.put或发布使用sinon mocking。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

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()

  })

})