我要使用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
的问题,但我无法弄清楚如何修复它。
答案 0 :(得分:0)
我意识到我错过了在对象上添加spy
。
jest.spyOn(pointsAwardingApi, 'awardPoints');