reactjs - 使用 sinon 进行 axios.put 或 post

标签 reactjs sinon

有谁知道我可以在其中找到 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/

相关文章:

javascript - 无法 stub 不存在的自有属性 findOne

reactjs - React useState - 我应该如何使用 setter 函数?

javascript - 如何延迟页面渲染直到从 api 收到数据

javascript - 模拟本地存储,用于测试两种方法的功能/覆盖

javascript - sinon.js stub - 如何 stub async.map

unit-testing - 尝试从单元测试的获取中模拟响应主体

reactjs - Gatsby.js : gatsby-plugin-google-analytics only firing events in production build

reactjs - React Native 样式中组件之间的空间

reactjs - 无状态 react 函数的首选语法是什么?为什么?

AngularJs 测试 Sinon spy