所以,我尝试对我的Redux组件进行一些测试,在这个测试中我只发送空obj {}
。并尝试在我的测试文件中将其与expect(get).toEqual(obj)
相等。我在错误日志中得到了什么:
$ yarn test
yarn run v1.5.1
warning ..\..\..\..\package.json: No license field
$ react-scripts test --env=jsdom
FAIL src\saga\saga.test.js
● get new action value
expect(received).toEqual(expected)
Expected value to equal:
{}
Received:
{}
Difference:
- Expected
+ Received
-Object {}
+ {}
at Object.<anonymous>.test (src/saga/saga.test.js:10:17)
at new Promise (<anonymous>)
at Promise.resolve.then.el (node_modules/p-map/index.js:46:16)
at <anonymous>
×获取新动作值(15ms)
我的test.js
文件:
import { put, call } from 'redux-saga/effects'
import { delay } from 'redux-saga'
import mySaga from './saga';
test('get new action value', () => {
const get = mySaga();
const data = 'hi1';
const obj = {};
expect(get).toEqual(obj);
});
我的saga.js文件:
import { takeEvery, put, call, take } from 'redux-saga/effects';
import { delay } from 'redux-saga';
function* createSaga(action) {
try {
yield call(delay, 1000);
yield put({type: "ADD_DAT", data: action.data + 1});
} catch (e) {
console.log(e)
}
};
function* mySaga() {
yield takeEvery("ADD_DATA", createSaga);
};
export default mySaga;
答案 0 :(得分:0)
当您致电const get = mySaga();
时,您要将mySaga
生成器的实例分配给变量get
。由于您使用正确的Jest匹配器toEqual
来比较两个对象,因此您的测试失败。
当比较预期值和实际值时,toEqual
匹配器将(最终)比较加固的对象原型。 get
的原型'[object Generator]'
不等于'[object Object]'
且您的测试失败。
正如评论中所建议的那样,请查看redux-saga testing docs以了解如何快速测试它们。由于您使用的是delay
,因此您可能还希望查看Timer Mocks的Jest文档,以帮助您伪造时间。