为了获得我的Saga文件100%的覆盖率,我正在研究如何测试观察者。
我一直在搜索,关于如何测试观察者有几个答案。也就是说,做takeEvery或takeLatest的传奇。
然而,所有的测试方法似乎基本上都复制了实现。那么,如果测试是相同的,那么编写测试有什么意义呢?
示例:
// saga.js
import { delay } from 'redux-saga'
import { takeEvery, call, put } from 'redux-saga/effects'
import { FETCH_RESULTS, FETCH_COMPLETE } from './actions'
import mockResults from './tests/results.mock'
export function* fetchResults () {
yield call(delay, 1000)
yield put({ type: FETCH_COMPLETE, mockResults })
}
export function* watchFetchResults () {
yield takeEvery(FETCH_RESULTS, fetchResults)
}试验方法1:
import { takeEvery } from 'redux-saga/effects'
import { watchFetchResults, fetchResults } from '../sagas'
import { FETCH_RESULTS } from '../actions'
describe('watchFetchResults()', () => {
const gen = watchFetchResults()
// exactly the same as implementation
const expected = takeEvery(FETCH_RESULTS, fetchResults)
const actual = gen.next().value
it('Should fire on FETCH_RESULTS', () => {
expect(actual).toEqual(expected)
})
})测试方法2:使用助手,如剩余Saga测试计划
这是一种不同的写作方式,但同样,我们所做的与实现基本相同。
import testSaga from 'redux-saga-test-plan'
import { watchFetchResults, fetchResults } from '../sagas'
import { FETCH_RESULTS } from '../actions'
it('fire on FETCH_RESULTS', () => {
testSaga(watchFetchResults)
.next()
.takeEvery(FETCH_RESULTS, fetchResults)
.finish()
.isDone()
})相反,我只想知道watchFestchResults是否拿走了每一个FETCH_RESULTS。甚至只有当它触发takeEvery()的时候。不管它如何跟进。
还是真的这样做?
发布于 2017-03-28 13:55:15
听起来,测试他们的目的是达到100%的测试覆盖率。
有一些东西你可以单元测试,但它是值得怀疑的,如果你应该。
在我看来,这种情况可能是一个更好的“集成”测试的候选人。不只是测试单个方法,而是测试几个方法是如何作为一个整体一起工作的。也许您可以调用一个操作,触发一个使用您的传奇的减速机,然后检查商店的结果变化?这将比单独测试传奇要有意义得多。
发布于 2017-07-16 14:56:56
我同意John的回答的观点,这比单元测试更适合集成测试。这个问题是基于up投票的GitHub中最受欢迎的。我建议你读一读。
其中一个建议是使用问题的开创者创建的redux-saga-测试器包。它有助于创建初始状态、启动佐贺助手(takeEvery、takeLatest)、分派saga正在侦听的操作、观察状态、检索操作历史记录和侦听发生的特定操作。
我正在与axios-模拟适配器一起使用它,但是在使用诺克的代码库中有几个例子。
传奇
import { takeLatest, call, put } from 'redux-saga/effects';
import { actions, types } from 'modules/review/reducer';
import * as api from 'api';
export function* requestReviews({ locale }) {
const uri = `/reviews?filter[where][locale]=${locale}`;
const response = yield call(api.get, uri);
yield put(actions.receiveReviews(locale, response.data[0].services));
}
// Saga Helper
export default function* watchRequestReviews() {
yield takeLatest(types.REVIEWS_REQUEST, requestReviews);
}使用Jest的测试示例
import { takeLatest } from 'redux-saga/effects';
import { types } from 'modules/review/reducer';
import SagaTester from 'redux-saga-tester';
import MockAdapter from 'axios-mock-adapter';
import axios from 'axios';
import watchRequestReviews, { requestReviews } from '../reviews';
const mockAxios = new MockAdapter(axios);
describe('(Saga) Reviews', () => {
afterEach(() => {
mockAxios.reset();
});
it('should received reviews', async () => {
const services = [
{
title: 'Title',
description: 'Description',
},
];
const responseData = [{
id: '595bdb2204b1aa3a7b737165',
services,
}];
mockAxios.onGet('/api/reviews?filter[where][locale]=en').reply(200, responseData);
// Start up the saga tester
const sagaTester = new SagaTester({ initialState: { reviews: [] } });
sagaTester.start(watchRequestReviews);
// Dispatch the event to start the saga
sagaTester.dispatch({ type: types.REVIEWS_REQUEST, locale: 'en' });
// Hook into the success action
await sagaTester.waitFor(types.REVIEWS_RECEIVE);
expect(sagaTester.getLatestCalledAction()).toEqual({
type: types.REVIEWS_RECEIVE,
payload: { en: services },
});
});
});https://stackoverflow.com/questions/42624587
复制相似问题