使用moxios匹配axios POST请求

时间:2017-04-17 11:22:04

标签: javascript axios moxios

是否可以使用moxios来模拟对POST请求的回复,该请求不仅可以通过URL匹配,还可以通过POST正文匹配?事后检查身体对我也有用。

这就是我现在正在做的事情。据我所知,没有特定方法的存根方法:

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

1 个答案:

答案 0 :(得分:7)

有一种方法可以使用moxios检查最后一个axios请求:

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

配置对象是在axios中传递的内容,data是请求的主体。