这是我的操作创建器:
export function signinUser({login, password}){
return function(dispatch){
axios.post('/auth/signin', {login, password})
.then((response)=>{
//-update state to indicate user is authenticated
var {username, userid, token} = response.data;
dispatch(authUser(token, username, userid));
console.log(1)
})
.catch(()=>{
//- show error message
dispatch(setErrorMessage('Bad Login Info'));
console.log(2)
});
}
}
测试:
describe('signinUser', ()=>{
var {signinUser} = actions;
var arg;
beforeEach(() => {
arg = {
login: 'john',
password: '123'
}
});
afterEach(function () {
nock.cleanAll()
});
it('should call setErrorMessage', ()=>{
nock('http://localhost:5000/auth/signin').post('')
.reply(401)
var expectedActions = [
{
type: 'SET_ERROR',
payload: 'Bad Login Info'
}
];
var store = mockStore({});
store.dispatch(signinUser({...arg}));
expect(store.getActions()).to.equal(expectedActions)
});
输出结果如下:
2
AssertionError: expected [] to equal [ Array(1) ]
这意味着 - 已经调用了操作创建者,并且 catch 中的 console.log(2) 已经被调用!但是,一些方式导致 getActions 返回一个空数组。请帮助我找出原因。