【发布时间】:2016-02-10 17:57:25
【问题描述】:
这是我要测试的控制器功能。
saveItem = (): void => {
this.updateItem();
this.loadingDialogService.showIndicator("Saving Item");
this._editItemService.updateItem(this.item).then((updatedItem: Item) => {
this.loadingDialogService.cancelDialog();
this.goToMainView();
}).catch(() => {
this.loadingDialogService.showErrorDialog("Failed to Save Item");
//this._log.error("Error CallingItemService");
});
}
这是我的测试:
it("should call method saveItem", () => {
spyOn(controller, 'updateItem');
spyOn(loadingDialogService, 'showIndicator');
spyOn(editItemService, 'updateItem').and.callFake(() => {
let result: Item
deferred.resolve(result);
return deferred.promise;
});
spyOn(loadingDialogService, 'cancelDialog');
spyOn(controller, 'goToMainView');
controller.saveItem();
expect(controller.updateItem).toHaveBeenCalled();
expect(loadingDialogService.showIndicator).toHaveBeenCalled();
expect(_editItemService.updateItem).toHaveBeenCalled();
expect(loadingDialogService.cancelDialog).toHaveBeenCalled();
expect(controller.goToMainView).toHaveBeenCalled();
});
测试在最后两个期望时失败,抛出错误提示
预期的间谍 cancelDialog 已被调用。
预计间谍 goToMainView 已被调用。
我猜测试没有执行 then 函数中的函数。有人能指出错误在哪里吗?
【问题讨论】: