【问题标题】:Using sinon for axios.put or post将 sinon 用于 axios.put 或 post
【发布时间】:2023-03-12 12:15:01
【问题描述】:

有谁知道链接或参考,我可以在其中找到 axios.put 或 post 的示例 sinon 测试? 目前我正在研究 react js,我想尝试使用 sinon 模拟测试我的 axios.put 或发布。任何帮助将不胜感激。

【问题讨论】:

    标签: reactjs sinon


    【解决方案1】:
    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()
    
      })
    
    })
    

    【讨论】:

      猜你喜欢
      • 2015-06-03
      • 1970-01-01
      • 1970-01-01
      • 2021-03-17
      • 1970-01-01
      • 2018-02-09
      • 2022-01-13
      • 2018-08-05
      • 2017-10-27
      相关资源
      最近更新 更多