gpt4 book ai didi

reactjs - redux - 如何使用 nock 测试异步操作

转载 作者:行者123 更新时间:2023-12-03 14:09:53 25 4
gpt4 key购买 nike

我按照 redux.org 的基本示例来测试异步操作

action.js

我的代码是这样的:

import axios from 'axios'

export function getGoodDataStart(){
return{
type: "GOOD_DATA_START"
}
}
export function getGoodDataSuccess(payload){
console.log('success', payload)
return {
type: "GOOD_DATA_SUCCESS",
payload: payload
}
}
export function getGoodDataFail(){
return{
type: "GOOD_DATA_FAIL"
}
}
export function getGoodData(){
return (dispatch) => {
dispatch( getGoodDataStart() )
return axios.get('http://www.google.com/list')
.then( response => {
console.log('fake res',response)
dispatch(getGoodDataSuccess (response) )
})
.catch( err => {
console.log('fake err',err)
})
}
}

测试.js

import nock from 'nock'
import React from 'react'
import {expect} from 'chai'
import {getGoodData} from 'registerAction'
import configureMockStore from 'redux-mock-store'
import thunk from 'redux-thunk'

const middlewares = [ thunk ]
const mockStore = configureMockStore(middlewares)

describe('Register component', () => {

it('async action', function () {

nock('http://www.google.com')
.get('/list')
.reply(200,'ok!' )

const store = mockStore({
myData: '' ,
})
const expected = [
{type: "GOOD_DATA_START"},
{type: "GOOD_DATA_SUCCESS", payload: 'ok!'}
]

return store.dispatch(getGoodData())
.then( () => {
expect(store.getActions()).to.equal(expected)
})
})
})

我遇到的问题是,nock 没有阻止请求,它让函数 getGoodData 向 google.com 发出真正的请求。我做错了什么?

错误的屏幕截图:

enter image description here

这是演示:https://github.com/craigcosmo/react-redux-test

安装:npm i

测试:npm run test

打开网址:http://localhost:5051/webpack-dev-server/

最佳答案

通常,在测试这样的操作时,您需要从等式中删除不属于您的操作的任何内容。在这种情况下,通过简单地使用 nock,您并没有从等式中删除 axios,而且实际上增加了不必要的复杂性。通过用 spy 模拟 axios,您可以避免进行网络调用,也可以完全避免调用 axios。这使您可以简单地断言使用正确的参数调用 axios。 spy 可以返回一个允许测试所有 promise 处理和后续操作调用的 promise 。为了演示这一点,我需要添加一个提供 spy 的库,因此我选择为断言和 spy 添加“expect”,但如果您想坚持使用 chai,您可以轻松地使用 sinon 做同样的事情。

这是一个示例,您根本不需要 nock,只需使用 spy 来模拟 axios:

import React from 'react'
import * as registerAction from 'registerAction'
import configureMockStore from 'redux-mock-store'
import thunk from 'redux-thunk'
import expect from 'expect'

const middlewares = [ thunk ]
const mockStore = configureMockStore(middlewares)

// set up to mock axios methods
import axios from 'axios'
const _get = axios.get

const fakePayload = { foo: 'bar' };
describe('Register component', () => {
beforeEach(() => {
// replace the .get method temporarily with a spy
axios.get = expect.createSpy().andReturn(Promise.resolve(fakePayload));
})

afterEach(() => {
// restore the get method with our saved const
axios.get = _get;
})

it('async action', function () {
const store = mockStore({
myData: '' ,
})
const expected = [
{type: "GOOD_DATA_START"},
{type: "GOOD_DATA_SUCCESS", payload: fakePayload}
]

return store.dispatch(registerAction.getGoodData())
.then( () => {
expect(store.getActions()).toEqual(expected)
expect(axios.get).toHaveBeenCalled()
expect(axios.get).toHaveBeenCalledWith('http://www.google.com/list')
})
})
})

关于reactjs - redux - 如何使用 nock 测试异步操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39565279/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com