【发布时间】:2020-03-05 16:51:54
【问题描述】:
问题说明:
我想使用 Jest 对 redux-saga 进行单元测试。我正在按照 redux-saga 文档中提供的示例中描述的方式执行此操作:https://redux-saga.js.org/docs/advanced/Testing.html
在我的 Saga 中,我调用了一个选择器函数 selectSet,它从应用商店返回一个特定的对象:
export const selectSet = state => state.setStore.set
在我的传奇中,我试图产生这个选择器函数:
import { put, select } from 'redux-saga/effects'
import { selectSet } from '../selectors'
export function* getSet() {
try {
const set = yield select(selectSet)
yield put({ type: 'SET_SUCCESS', payload: { set } })
} catch (error) {
yield put({ type: 'SET_ERROR', payload: { error } })
}
}
在我的测试中,没有有效的应用商店,所以我必须模拟该函数以返回预期的对象:
import assert from 'assert'
import * as AppRoutines from './AppRoutines'
import { put, select } from 'redux-saga/effects'
describe('getSet()', () => {
it('should trigger an action type "SET_SUCCESS" with a payload containing a valid set', () => {
const generator = AppRoutines.getSet()
const set = {
id: 1,
slots: [{ id: 1 }, { id: 2 }, { id: 3 }, { id: 4 }, { id: 5 }],
}
const selectSet = jest.fn()
selectSet.mockReturnValue(set)
// Saga step 1
const actualFirst = generator.next().value
const expectedFirst = select(selectSet)
assert.deepEqual(
actualFirst,
expectedFirst,
'it should retreive a valid set from the store using the selectSet selector'
)
})
})
但是 - 如果我断言 saga 使用 deepEqual 和我的模拟函数返回特定的生成器值,它希望我的选择器函数具有原始的 selectSet 构造函数。但由于我用 jest.fn() 模拟函数,因此构造函数实际上等于 mockConstructor - 这使我的测试失败:
Expected value to deeply equal to:
{"@@redux-saga/IO": true, "SELECT": {"args": Array [], "selector": [Function mockConstructor]}}
Received:
{"@@redux-saga/IO": true, "SELECT": {"args": Array [], "selector": [Function selectSet]}}
问题:我怎样才能使 assert.deepEqual 包含一个模拟函数而不冲突的构造函数类型?
替代问题:有没有办法让我的断言期待 mockConstructor 而不是实际的 selectSet 构造函数?
【问题讨论】:
标签: javascript unit-testing react-redux jestjs redux-saga