angularjs – Angular Jasmine测试函数调用然后
发布时间:2020-12-17 17:25:53 所属栏目:安全 来源:网络整理
导读:这是我要测试的控制器功能. saveItem = (): void = { this.updateItem(); this.loadingDialogService.showIndicator("Saving Item"); this._editItemService.updateItem(this.item).then((updatedItem: Item) = { this.loadingDialogService.cancelDialog();
这是我要测试的控制器功能.
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(); }); 测试在最后两次预期失败,抛出错误说
我猜测试不执行函数内部的函数.有人可以指出错误在哪里吗? 解决方法
您有解决的承诺,因此您需要在函数调用之后但在测试之前运行摘要循环.
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(); $scope.$digest(); expect(controller.updateItem).toHaveBeenCalled(); expect(loadingDialogService.showIndicator).toHaveBeenCalled(); expect(_editItemService.updateItem).toHaveBeenCalled(); expect(loadingDialogService.cancelDialog).toHaveBeenCalled(); expect(controller.goToMainView).toHaveBeenCalled(); }); 话虽如此,你的测试后来会引发你的问题,因为它有5个断言(expect()s).当一个人失败时,你将不得不浪费时间搞清楚它是哪一个.坚持每次测试一次断言(OAPT.)这应该是5次测试,每次都有一个断言.这样,当某些事情失败时,你就会知道它是什么. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |