我正在尝试测试以下功能。
function doThing(client, id) {
return (dispatch, getState) => {
dispatch(getThingRequest());
const {session} = getState();
return client.getThing(session, id)
.then(thing => {
dispatch(getThingSuccess(thing));
})
.catch(error => {
dispatch(getThingFailure(error));
});
};
}
function getThingRequest() {
return {
type: 'GET_THING_REQUEST'
};
}
function getThingSuccess(thing) {
return {
type: 'GET_THING_SUCCESS',
thing
};
}
function getThingFailure(error) {
return {
type: 'GET_THING_FAILURE',
error
};
}
这是我到目前为止所拥有的
test('get thing success', function() {
const client = {getThing: function() {}};
const thing = {'a': 123};
sinon.stub(client, 'getThing').returns(Promise.resolve(thing));
const fn = getThing(client, '1');
equal(typeof fn, 'function');
const dispatch = sinon.spy();
const getState = () => ({session: {}});
fn(dispatch, getState)
dispatch.getCall(0).calledWith({ type: 'GET_THING_REQUEST' });
dispatch.getCall(1).calledWith({ type: 'GET_THING_SUCCESS', thing: thing });
});
我得到的错误如下,但我不知道为什么会这样。
TypeError:'null'不是对象(评估 'dispatch.getCall(1).calledWith')