已调用的预期模拟函数。但它没有被召集。开玩笑

时间:2017-09-09 18:02:03

标签: javascript testing promise jestjs

我要使用jest测试以下功能。

import * as pointsAwardingApi from '../api/pointsAwardingApi';
export function awardPoints(pointsAwarding) {
  return function (dispatch) {
    return new Promise((resolve, reject) => {
      pointsAwardingApi.awardPoints(pointsAwarding);
    });
  };
}

然后,我创建了以下测试。

import * as pointsAwardingApi from '../../../src/api/pointsAwardingApi';
it("should call award points api", () => {
  //given
  pointsAwardingApi.awardPoints = jest.fn();
  let dispatcher = pointsAwardingActions.awardPoints({phone: '555'});

  //when
  dispatcher(mockedDispatch);

  //then
  expect(pointsAwardingApi.awardPoints).toBeCalledWith({phone: '555'});
});

但是,当我运行测试时,我收到以下错误。

expect(jest.fn()).toBeCalledWith(expected)
Expected mock function to have been called with: [{"phone": "555"}]
But it was not called.

我想这是Promise的问题,但我无法弄清楚如何修复它。

1 个答案:

答案 0 :(得分:0)

我意识到我错过了在对象上添加spy

jest.spyOn(pointsAwardingApi, 'awardPoints');