我正在尝试使用redux-saga-test-plan来理解测试,但我有点被动态负载所困。
这是我的传奇故事:
function* getCheapCars() {
try {
const response = yield call(fetch, '/api/home')
const cars = yield call([response, response.json])
yield put({ type: types.FETCH_DATA_SUCCESS, cars })
} catch (error) {
yield put({ type: types.FETCH_DATA_ERROR, error })
}
}下面是测试结果:
it('just works!', () => {
return expectSaga(saga)
.take('FETCH_DATA_SUCCESS')
.put({ type: 'FETCH_DATA_SUCCESS', cars })
.dispatch({ type: 'FETCH_DATA_SUCCESS', cars })
.run()
})我可能错了(我对saga完全陌生),但如何从request中获取动态有效负载--在本例中是cars。Cars表示多个“post”数组,其中包含title、slug、id和许多其他字段。我应该只创建一个带有完全相同字段的伪对象吗?
我需要一点帮助来测试withReducer,基本上这里是相同的saga和这个测试通过,但它不应该。因为我不期望一个空的项目数组,而是长度为4的数组(汽车)。
it('just works!', async () => {
const { storeState } = await expectSaga(saga)
.withReducer(reducer)
.run()
expect(storeState).toEqual({
loading: false,
error: null,
items: []
})
})是不是有什么我不明白的?提前谢谢。
发布于 2018-08-16 19:06:23
我在使用redux- saga -test-plan进行saga测试时所理解的是,我们应该模拟我们的api调用并选择调用。
模拟是通过provide方法完成的,因此不会调用真正的api。
const mockResponse = []; // your api response
it('getCheapCars test', () => expectSaga(getCheapCars)
.provide([
[call(fetch, '/api/home'), mockResponse],
])
.put({ type: types.FETCH_DATA_SUCCESS, cars: mockResponse })
.run())https://stackoverflow.com/questions/51686072
复制相似问题