我如何在Redux-Sagas中进行测试

时间:2019-01-19 02:26:28

标签: javascript firebase redux firebase-authentication redux-saga

我正在使用Firebase,因此我将auth作为依赖项注入。在我的sagas中,我有以下代码:

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)))
})
})

这应该可行,因为传奇正常工作,但是出现以下错误:

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.

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

1 个答案:

答案 0 :(得分:0)

expect(JSON.stringify(result)).toEqual(
  JSON.stringify(call([authMock, authFunction]))
);
相关问题