首页 > 解决方案 > 我如何在 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.

任何人都知道我该如何解决这个问题?我整天都在尝试解决这个问题,但它不起作用。

标签: javascriptfirebasereduxfirebase-authenticationredux-saga

解决方案


expect(JSON.stringify(result)).toEqual(
  JSON.stringify(call([authMock, authFunction]))
);

推荐阅读