使用moxios匹配axios POST请求 [英] Matching axios POST request with moxios

查看:322
本文介绍了使用moxios匹配axios POST请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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


推荐答案

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

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)

配置对象是在axios中传递的内容,数据是请求的正文。

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

这篇关于使用moxios匹配axios POST请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆