我在测试这个传奇
export function* foo() {
yield put(actions.start());
yield put(actions.bar({
onSuccess: () => {
// do something
},
onFailed: () => {
// do something else
}
}));
yield put(userActions.done());
}这是我的测试
it('should test foo saga, and put start, bar and done actions', () => {
// assertions / expect
testSaga(sagas.foo)
.next()
.put(actions.start())
.next()
.put(
actions.bar({
onSuccess: () => {},
onFailed: () => {},
}),
)
.next()
.put(actions.done())
.next()
.isDone();
});当我从saga中删除有效负载并测试它通过时,没有问题,但是当我添加有效负载(不仅仅是onSuccess和onFailed回调)时,它会显示这个错误。
Assertion failed: put effects do not match
Expected
--------
{
channel: null,
action:
{ type: 'BAR',
payload:
{
onSuccess: [Function: onSuccess],
onFailed: [Function: onFailed]
}
}
}
Actual
------
{
channel: null,
action:
{ type: 'BAR',
payload:
{
onSuccess: [Function: onSuccess],
onFailed: [Function: onFailed]
}
}
}有趣的是,实际和预期的有效载荷是相等的,但是测试没有通过!
发布于 2021-06-23 10:01:27
onSuccess和onFailed方法在saga和测试用例中有不同的引用。这些断言肯定会失败。
由于您在saga中声明了这两个方法,所以我们无法通过模块要求将它们导入测试用例中。因此,我们无法在测试用例中使用这两种方法的相同引用。
我们可以使用检查通用断言,由redux-saga-test-plan提供。
如果您的传奇产生了一种不确定类型的值,或者一些不容易被效果断言或其他一般断言覆盖的东西,那么您可以使用
inspect检索实际产生的值,并使用您喜欢的断言库执行自己的断言。
我们使用它来获得yield put(actions.bar({...}))的返回效果。然后,我们可以在测试用例中获得由actions.bar({...})创建的redux操作,包括onSuccess、onFailed方法,以及您在actions.bar()动作创建者中传递的所有内容。
我们可以使用jestjs提供的expect.any(Function)断言这两种方法。您甚至可以执行和测试它们。
例如。
saga.ts
import { put } from 'redux-saga/effects';
import * as actions from './actions';
export function* foo() {
yield put(actions.start());
yield put(
actions.bar({
onSuccess: () => {
// do something
},
onFailed: () => {
// do something else
},
}),
);
yield put(actions.done());
}saga.test.ts
import { testSaga } from 'redux-saga-test-plan';
import { foo } from './saga';
import * as actions from './actions';
import { PutEffect } from 'redux-saga/effects';
import { AnyAction } from 'redux';
describe('54885611', () => {
it('should pass', () => {
const logSpy = jest.spyOn(console, 'log');
testSaga(foo)
.next()
.put(actions.start())
.next()
.inspect<PutEffect<AnyAction>>((yieldedValue) => {
expect(yieldedValue.payload.action).toEqual({
type: 'START',
payload: expect.objectContaining({ onSuccess: expect.any(Function), onFailed: expect.any(Function) }),
});
// test onSuccess
yieldedValue.payload.action.payload.onSuccess();
expect(logSpy).toBeCalledWith('do something');
// test onFailed
yieldedValue.payload.action.payload.onFailed();
expect(logSpy).toBeCalledWith('do something else');
logSpy.mockRestore();
})
.next()
.put(actions.done())
.next()
.isDone();
});
});单元测试结果:
PASS src/stackoverflow/54885611/saga.test.ts
54885611
✓ should pass (25 ms)
console.log
do something
at console.<anonymous> (node_modules/jest-mock/build/index.js:848:25)
console.log
do something else
at console.<anonymous> (node_modules/jest-mock/build/index.js:848:25)
------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
------------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
actions.ts | 100 | 100 | 100 | 100 |
saga.ts | 100 | 100 | 100 | 100 |
------------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 3.351 shttps://stackoverflow.com/questions/54885611
复制相似问题