Matching axios POST request with moxios

扶醉桌前 提交于 2020-01-03 10:44:17

问题


Is it possible to use moxios to mock a reply to a POST request that will match not just by URL but also by the POST body? Inspecting the body afterwards would work for me too.

This is what I am doing now. As far as I know there are no method specific stubbing methods:

describe('createCode', function () {
    it('should create new code', function () {
        moxios.stubRequest(process.env.API_URL + '/games/GM01/codes', {
            status: 200
        })
    })
})

回答1:


There is a way to inspect the last axios request using moxios:

let request = moxios.requests.mostRecent()
expect(request.config.method).to.equal('post')
expect(JSON.parse(request.config.data)).to.deep.equal(code)

The config object is what's being passed in axios, data is the body of the request.



来源:https://stackoverflow.com/questions/43450461/matching-axios-post-request-with-moxios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!