javascript - 我如何在 redux-sagas 中进行测试
问题描述
我正在使用firebase,所以我将auth
其作为依赖注入。在我的传奇中,我有这个代码:
export function* isAuth(auth) {
try{
const wrapper = {
authFunction : () => auth.currentUser
}
const {authFunction} = wrapper
const user = yield call([wrapper, authFunction])
if (user !== null){
yield put(ActionCreator.authSuccess(user))
}
}catch({message}){
yield put(ActionCreator.authFailure(message))
}
}
在我的测试文件中,我有以下代码:
describe('should test isAuth', () => {
const auth = {currentUser: {}}
const {currentUser} = auth
const authMock = {
authFunction: () => currentUser
}
const {authFunction} = authMock
const it = sagaHelper(isAuth(authMock))
it('should call api authFunction', result => {
expect(result).toEqual(call([authMock, authFunction]))
return {
user: undefined
}
})
it('should put authSuccess', result => {
expect(result).toEqual(put(ActionCreator.authSuccess(undefined)))
})
})
这应该可以工作,因为 saga 可以正常工作,但是会出现以下错误:
should test isAuth › should call api authFunction
expect(received).toEqual(expected)
Expected value to equal:
{"@@redux-saga/IO": true, "CALL": {"args": [], "context": {"authFunction": [Function authFunction]}, "fn": [Function authFunction]}}
Received:
{"@@redux-saga/IO": true, "CALL": {"args": [], "context": {"authFunction": [Function authFunction]}, "fn": [Function authFunction]}}
Difference:
Compared values have no visual difference.
任何人都知道我该如何解决这个问题?我整天都在尝试解决这个问题,但它不起作用。
解决方案
expect(JSON.stringify(result)).toEqual(
JSON.stringify(call([authMock, authFunction]))
);
推荐阅读
- string - 删除具有两个连续字母的给定字符串的一个字母
- graphql - 如何使用 Apollo 对我的 GraphQL 查询进行重复数据删除?
- git - 是否可以合并同一项目的两个不同的 git 存储库?
- postgresql - 通过命令行将变量传递给 psql 文件。一个变量通过其他不通过
- c# - 删除可以包含分隔符的字符串中分隔符之间的文本
- html - 图片属性高度:100% 不起作用
- reactjs - 使用 Reactjs 切换侧边栏菜单
- jquery - 在 Gatsby 中使用 jQuery 和外部脚本
- jquery - jQuery Next 直到当前状态隐藏/显示子 TR
- ssh - 在 Airflow 的 python_callable 中执行 SSH 连接?