【发布时间】:2020-01-11 14:20:50
【问题描述】:
我正在尝试测试我的 reducer,但是当我在测试中分派操作时,我的初始状态没有改变。
那是我的减速机:
export default function login(state = INITIAL_STATE, action) {
switch (action.type) {
case Types.SET_ERROR:
return {
...state,
error: action.error,
};
case Types.SET_LOGIN:
return {
...state,
user: {email: action.user.email, password: action.user.password},
};
default:
return state;
}
}
这是我的测试:
it('should handle SET_ERROR', () => {
const setErrorAction = {type: 'SET_ERROR', error: 'a'};
expect(login(INITIAL_STATE, setErrorAction)).toEqual({
...INITIAL_STATE,
error: 'a',
});
});
这是我的测试结果:
expect(received).toEqual(expected) // deep equality
- Expected
+ Received
Object {
- "error": "a",
+ "error": "",
"user": Object {
"email": "",
"password": "",
},
}
【问题讨论】:
-
你确定
Types.SET_ERROR=== 'SET_ERROR' 吗? -
你是对的!感谢您帮助我,我没有注意到。
-
欢迎使用解决此问题的确切解决方案来回答您的问题!
标签: reactjs react-native testing redux jestjs