使用Promise进行测试时,Sinon .getCall(1)返回null

时间:2016-01-15 19:30:22

标签: javascript unit-testing promise sinon

我正在尝试测试以下功能。

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

0 个答案:

没有答案