标签: moxios

使用moxios匹配axios POST请求

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

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

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

javascript axios moxios

6
推荐指数
1
解决办法
3540
查看次数

如何使用 axios-mock-adapter 等待请求完成,就像使用 moxios 一样?

我尝试在从服务器获取某些内容后测试其渲染。我使用Vue Test Utils但这无关紧要。

created组件的钩子中,ajax 调用是通过axios. 我注册axios-mock-adapter响应并“渲染”组件,进行调用,一切正常,但我必须使用该moxios库来等待请求完成。

it('displays metrics', (done) => {

  this.mock.onGet('/pl/metrics').reply((config) => {
    let value = 0
    if (config.params.start == '2020-01-26') {
      value = 80
    }
    if (config.params.start == '2020-01-28') {
      value = 100
    }
    return [200, {
      metrics: [
        {
          key: "i18n-key",
          type: "count",
          value: value
        }
      ]
    }]
  })
  .onAny().reply(404)

  let wrapper = mount(Dashboard)

  moxios.wait(function() {
    let text = wrapper.text()
    expect(text).toContain('80')
    expect(text).toContain('100')
    expect(text).toContain('+20')
    done()
  })
})
Run Code Online (Sandbox Code Playgroud)

是否有可能摆脱 …

jasmine moxios axios-mock-adapter

5
推荐指数
1
解决办法
3982
查看次数

使用 Jest、Vue Test Utils 和 Moxios 测试 VueJS 时,`moxios.wait` 从不执行

我正在尝试为 VueJS 组件编写我的第一个单元测试,该组件在呈现时从 API 端点获取一些数据:

我的 VueJS 组件:

import axios from 'axios';

export default {
  props: {
          userIndexUrl: {
            required: true
          }
        },
  data() {
    userList: [],
    tableIsLoading: true
  },
  created() {
    const url = this.userIndexUrl;
    axios.get(url)
    .then(response => {
      this.userList = response.data.data;
      this.tableIsLoading = false;
    })
  },
}
Run Code Online (Sandbox Code Playgroud)

和我的测试:

import { mount } from 'vue-test-utils'
import moxios from 'moxios'
import UsersAddRemove from 'users_add_remove.vue'

describe('UsersAddRemove', () => {
  const PROPS_DATA = {
      userIndexUrl: '/users.json'
  }

  beforeEach(() => {
    moxios.install();
    moxios.stubRequest('/users1.json', …
Run Code Online (Sandbox Code Playgroud)

vue.js moxios

4
推荐指数
1
解决办法
2333
查看次数

Moxios - TypeError:无法读取未定义的属性“适配器”

尝试测试axios调用并尝试moxios包。

"axios": "^0.16.2", "moxios": "^0.4.0",

在这里找到:https : //github.com/axios/moxios

按照那里的例子,但我的测试错误就moxios.install()行了:

import axios from 'axios'
import moxios from 'moxios'
import sinon from 'sinon'
import { equal } from 'assert'

describe('mocking axios requests', function () {

  describe('across entire suite', function () {

    beforeEach(function () {
      // import and pass your custom axios instance to this method
      moxios.install()
    })
Run Code Online (Sandbox Code Playgroud)

我的实际测试

import axios from 'axios';
import moxios from 'moxios';
import sinon from 'sinon';
import { equal } from …
Run Code Online (Sandbox Code Playgroud)

testing axios moxios

3
推荐指数
1
解决办法
3833
查看次数

使用Moxios模拟Axios调用以测试API调用

我有以下自定义Axios实例:

import axios from 'axios'

export const BASE_URL = 'http://jsonplaceholder.typicode.com'

export default axios.create({
  baseURL: BASE_URL
})
Run Code Online (Sandbox Code Playgroud)

配合相应的服务:

import http from './http'

export async function fetchUserPosts(id) {
  const reponse = await http.get(`/users/${id}/posts`)
  return reponse.data
}
Run Code Online (Sandbox Code Playgroud)

这是对上述服务的测试:

import moxios from 'moxios'
import sinon from 'sinon'
import http from '@/api/http'
import { fetchUserPosts } from '@/api/usersService'

describe('users service', () => {
  beforeEach(() => {
    moxios.install(http)
  })

  afterEach(() => {
    moxios.uninstall(http)
  })

  it('fetches the posts of a given user', (done) => {
    const id …
Run Code Online (Sandbox Code Playgroud)

javascript unit-testing axios moxios

2
推荐指数
1
解决办法
8956
查看次数

Moxios:如何访问不是单元测试中最新的请求

有一个页面在渲染时发出多个HTTP获取数据请求。如何过滤moxios.requests以获取具有特定URL的请求以进行响应?

我可以在网上找到的唯一方法是moxios.requests.mostRecent(),但是我不确定最近的请求是什么。即使这样,还有另一种方法可以弹出另一个请求吗?

此处的moxios库上似乎没有任何文档:https : //github.com/axios/moxios

 let request = moxios.requests.mostRecent() // How to get 'some/url/' here? This expect is currently failing
      expect(request.url).to.equal('some/url/')
      request.respondWith({
        status: 200,
        response: []
}
Run Code Online (Sandbox Code Playgroud)

axios moxios

2
推荐指数
1
解决办法
2200
查看次数

在行为回调中包装异步 moxios 调用

我正在尝试使用钩子测试反应功能组件。useEffect 钩子调用第三方 API,然后在返回时调用 setState。

我有测试工作,但不断收到警告,指出组件的更新未包含在行为中。

我遇到的问题是期望在 moxios.wait 承诺内,因此我无法将其包装在行为函数中,然后对其结果进行断言。

测试通过了,但我知道不包装在行为函数中更新状态的代码可能会导致误报或未发现的错误。我只是想知道我应该如何测试这个。

我已经尝试在 react 16.9.0 alpha 版本中使用新的 async await act 函数,以及我在许多 github 问题(如 jest setTimers)中发现的许多建议,但似乎都没有解决问题。

组件

 const Benefits = props => {
  const [benefits, setBenefits] = useState([])
  const [editing, setEditing] = useState(false)
  const [editingBenefit, setEditingBenefit] = useState({id: null, name: '', category: ''})

  useEffect(() => {
    axios.get('#someurl')
      .then(response => {
        setBenefits(response.data)
    })
  }, [])
}
Run Code Online (Sandbox Code Playgroud)

考试

describe('Benefits', () => {
  it('fetches the list of benefits from an api and populates the benefits table', (done) …
Run Code Online (Sandbox Code Playgroud)

reactjs moxios react-testing-library

2
推荐指数
1
解决办法
1095
查看次数