我正在尝试使用redux-saga-test-plan库测试我的redux-sagas,以简化操作。所有测试都在JEST环境中运行。
为了帮助我在分叉的传奇上构建我的测试,我遵循了以下文档:https://github.com/jfairbank/redux-saga-test-plan/blob/master/docs/integration-testing/forked-sagas.md
但是当我尝试运行测试时,我得到了这个错误:
TypeError: Cannot read property 'name' of undefined
12 | .put(setMenu(menuList))
13 | .dispatch('SET_MENU_REQUEST', menuList)
> 14 | .run()
15 | })
16 |
我的测试(已更新):
import { expectSaga } from 'redux-saga-test-plan'
import { call, put, takeLatest, takeEvery } from "redux-saga/effects";
import menuSaga from '../../sagas/MenuSaga'
import { applyMenu } from '../../sagas/MenuSaga'
import { menuList } from '../../stubs/menuList'
import { setMenu } from '../../reducers/menuReducer'
it("Sets a new menu", () => {
return expectSaga(applyMenu)
.put(setMenu(menuList))
.dispatch('SET_MENU_REQUEST', menuList)
.run()
})
更新 我发现了一些documentation关于如何利用效果助手(takeEvery,takeLatest)并尝试过,但没有效果,只会引起新的错误。
使用effect-helper进行测试:
it("Sets a new menu", () => {
return expectSaga(menuSaga)
.next()
.put(setMenu(menuList))
.takeEvery('SET_MENU_REQUEST', applyMenu)
.finish()
.isDone()
})
它抛出的错误:
TypeError: (0 , _reduxSagaTestPlan.expectSaga)(...).next is not a function
10 |
11 | return expectSaga(menuSaga)
> 12 | .next()
13 | .put(setMenu(menuList))
14 | .takeEvery('SET_MENU_REQUEST', applyMenu)
15 | .finish()
我试图测试的传奇:
import {fork, put, select, call} from 'redux-saga/effects'
import {takeEvery, takeLatest} from 'redux-saga'
import { handleRequest } from './serverSaga'
import { setMenu } from '../reducers/menuReducer'
const POST_MENU_REQUEST = 'POST_MENU_REQUEST'
const GET_MENU_REQUEST = 'GET_MENU_REQUEST'
const SET_MENU_REQUEST = 'SET_MENU_REQUEST'
export function setMenuRequest(menu) {return {type: SET_MENU_REQUEST, menu}}
export function postMenuRequest(data) {return {type: POST_MENU_REQUEST, data}}
export function getMenuRequest() {return {type: GET_MENU_REQUEST}}
export function* applyMenu(menu) {
yield put(setMenu(menu))
}
function* postNewMenu(data){
yield call(handleRequest, '/admin/menu' ,'POST', data)
}
function* getMenu(){
let response = yield call(handleRequest, "/admin/menu", "GET")
if(response){
yield put(setMenu(response))
}
}
export default function* menuSaga(){
yield [
fork(function*() {
yield takeEvery(POST_MENU_REQUEST, postNewMenu)
}),
fork(function*() {
yield takeEvery(GET_MENU_REQUEST, getMenu)
}),
fork(function*(){
yield takeEvery(SET_MENU_REQUEST, applyMenu)
})
]
}
正如你所看到的,我试图导出我想要测试的生成器函数,即使我不认为这是最优的,因为测试不应该要求我改变我的代码只是为了测试它,所以我可能错过了一些东西。
stub menuList的结构如下:
Date: '',
Id: '',
ClosingTime: '',
MenuItems: [],
事实证明,测试我的传奇并不是那么严格,因为它们都是这样的结构(分叉),并且似乎没有很多关于如何正确测试它们的文档。
答案 0 :(得分:1)
可能是您的问题是,您从redux-saga-test-plan导入并使用 expectSaga 时,应该是 testSaga
答案 1 :(得分:0)
问题是您正在将redux-saga-test-plan的单元测试与其集成测试混合在一起。
expectSaga(menuSaga) // this is an integration test function
.next() // this is a unit test function
.put(setMenu(menuList))
.takeEvery('SET_MENU_REQUEST', applyMenu)
.finish()
请参阅:https://redux-saga-test-plan.jeremyfairbank.com/integration-testing/