【发布时间】:2017-09-09 18:02:03
【问题描述】:
我想用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 的问题,但我不知道如何解决。
【问题讨论】:
标签: javascript testing promise jestjs