如何在angularjs中使用$ promise.then函数对方法进行单元测试-TypeError:无法读取未定义的属性'then'

时间:2019-01-14 17:44:39

标签: javascript angularjs unit-testing karma-jasmine

我有一个方法,并且带有promise.then函数。

我不确定如何测试该带注释的零件。当我运行带有带注释的零件测试的测试时,我得到了 TypeError:无法读取未定义的属性'then'。如何解决此问题?

键入脚本代码:

showHistory(): void {
	let title = "";
	let action = null;
	let id = -1;

	switch (this.selectedIndex) {
	case 0:
		title = this.$translate.instant("dx.dxHistory");
		action = this.history.getDefHistory;
		break;
	case 1:
		title = this.$translate.instant("dx.stHistory");
		action = this.history.getStHistory;
		break;
	}

	// action({dxId: id}).$promise.then((result) => {
	//     const options = {
	//         fields: [
	//             {key: "changedBy", title: this.$translate.instant("dx.changedBy")}
	//         ]
	//     };
	//
	//     this.$mdDialog.show({
	//         clickOutsideToClose: true
	//     });
	// });
}

测试代码:

describe("show history", () => {
    
    	fit("should set .......", () => {
    		dxTabs.selectedIndex = 0;
    		dxTabs.dx = createdx();
    
    		const action = () => {
    			return {
    				$promise: $q.when([])
    			};
    		};
    
    		spyOn($translate, "instant").and.callFake((str) => {
    			expect(str).toEqual("dx.dxHistory");
    			return "test";
    		});
    		spyOn(history, "getDefHistory").and.returnValue(action);
    
    		dxTabs.showHistory();
    		expect($translate.instant).toHaveBeenCalled();
    	});
    });

0 个答案:

没有答案