单元测试 redux-saga select
Unit testing redux-saga select
我有一个这种形式的简单传奇:
const getAccountDetails = function * () {
const { url } = yield select(state => state.appConfig)
const accountDetails = yield call(apiFetchAccountDetails, url)
}
我正在尝试编写单元测试:
describe('getAccountDetails', () => {
const iterator = getAccountDetails()
it("should yield an Effect 'select(state=> state.appConfig)'", () => {
const effect = iterator.next().value
const expected = select(state => state.appConfig)
expect(effect).to.deep.eql(expected)
})
此测试失败。尽管 effect
和 expected
非常相似,但并不完全相同。
在payload.selector.scopes
中至少隐藏了一个差异,其中产生的效果和预期如下:
由于这两者的范围总是不同的,如何才能使这些测试起作用?
eta:此模式改编自 the example 链接到 redux-saga 文档
从回来的路上找到 this issue 后破解了它。
修复方法是创建一个命名函数来执行 select 并将其从被测传奇所在的模块中导出,然后在测试中使用相同的函数。一切顺利。
export const selectAppConfig = state => state.appConfig
const getAccountDetails = function * () {
const { url } = yield select(selectAppConfig)
const accountDetails = yield call(apiFetchAccountDetails, url)
}
import {selectAppConfig} from './sagaToTest'
describe('getAccountDetails', () => {
const iterator = getAccountDetails()
it("should yield an Effect 'select(state=> state.appConfig)'", () => {
const effect = iterator.next().value
const expected = select(selectAppConfig)
expect(effect).to.deep.eql(expected)
})
我有一个这种形式的简单传奇:
const getAccountDetails = function * () {
const { url } = yield select(state => state.appConfig)
const accountDetails = yield call(apiFetchAccountDetails, url)
}
我正在尝试编写单元测试:
describe('getAccountDetails', () => {
const iterator = getAccountDetails()
it("should yield an Effect 'select(state=> state.appConfig)'", () => {
const effect = iterator.next().value
const expected = select(state => state.appConfig)
expect(effect).to.deep.eql(expected)
})
此测试失败。尽管 effect
和 expected
非常相似,但并不完全相同。
在payload.selector.scopes
中至少隐藏了一个差异,其中产生的效果和预期如下:
由于这两者的范围总是不同的,如何才能使这些测试起作用?
eta:此模式改编自 the example 链接到 redux-saga 文档
从回来的路上找到 this issue 后破解了它。
修复方法是创建一个命名函数来执行 select 并将其从被测传奇所在的模块中导出,然后在测试中使用相同的函数。一切顺利。
export const selectAppConfig = state => state.appConfig
const getAccountDetails = function * () {
const { url } = yield select(selectAppConfig)
const accountDetails = yield call(apiFetchAccountDetails, url)
}
import {selectAppConfig} from './sagaToTest'
describe('getAccountDetails', () => {
const iterator = getAccountDetails()
it("should yield an Effect 'select(state=> state.appConfig)'", () => {
const effect = iterator.next().value
const expected = select(selectAppConfig)
expect(effect).to.deep.eql(expected)
})