gpt4 book ai didi

javascript - 测试调用服务返回 promise 的 Angular Controller

转载 作者:行者123 更新时间:2023-11-30 17:11:43 25 4
gpt4 key购买 nike

我正在尝试测试一个依赖于服务的 Angular Controller ,并且服务方法返回 promise 。我正在创建一个 Jasmine spy 对象来模拟服务和返回 promise 的方法。但出于某种原因,我的模拟 promise 没有返回已解决的结果。

这是我的 Controller 和服务代码。

(function(){
'use strict';
angular
.module("supportPortal",[])
.service('TipsService' ,['$http' ,TipsService])
.controller('TipsCtrl', [ 'TipsService', TipsCtrl]);

function TipsService($http) {
this.path = 'api/bondtipsfactor';
this.tipsFactors = [];
this.getMinMaxDates = getMinMaxDates;
this.getData = getData;

function getMinMaxDates() {

var self = this;
var promise = $http.get(self.path + '/minmaxdate').then(function (result) {
return result.data;
});
return promise;
}
}

function TipsCtrl(TipsService) {
/* jshint validthis:true */

var vm = this,
svc = TipsService;
vm.title = 'TipsCtrl';
vm.setMonths = setMonths;
var today = new Date();
vm.minMonth = 1;
vm.minYear = today.getFullYear();
vm.maxYear = today.getFullYear();
vm.maxMonth = today.getMonth() + 1;
vm.years = [];
vm.months = [];
vm.selectedYear = 2014;
vm.selectedMonth;
activate();
function activate() {
svc.getMinMaxDates().then(function (data) {
console.log(data);
var minDate = new Date(data.MinDate),
maxDate = new Date(data.MaxDate);
maxDate.setMonth(maxDate.getMonth() + 1);
vm.minMonth = minDate.getMonth();
vm.minYear = minDate.getFullYear();
vm.maxMonth = maxDate.getMonth();
vm.maxYear = maxDate.getFullYear();
for (var i = vm.minYear; i <= vm.maxYear; i++) {
vm.years[i - vm.minYear] = i;
}
});
}

function setMonths(year) {
var startMonth = year === vm.minYear? vm.minMonth: 1,
endMonth = year === vm.maxYear ? vm.maxMonth : 12;
vm.month=[];
for (var i = startMonth; i <= endMonth; i++) {
vm.months[i - startMonth] = i;
}
}
}
})();

这是测试代码

describe("TipsCtrlSpec", function () {
describe("TipsCtrl", function () {

var ctrl, service, $q, $controller;
beforeEach(angular.mock.module("supportPortal", function($provide) {
service = jasmine.createSpyObj("TipsService", ['getMinMaxDates']);
$provide.value("TipsService", service);
}));

beforeEach(inject(function (_$controller_, _$q_, _TipsService_) {
service = _TipsService_;
$q = _$q_;
$controller = _$controller_;
}));

function createController(resolve)
{
var deferred = $q.defer();
service.getMinMaxDates.and.returnValue(deferred.promise);
ctrl = $controller("TipsCtrl", {
TipsService: service
});
if (resolve) {
deferred.resolve({
MinDate: "01/01/2013",
MaxDate: "01/01/2014"
});
} else {
deferred.reject();
}
}

it("activate sets min max dates", function () {
createController(true);
expect(ctrl).toBeDefined();
expect(service.getMinMaxDates).toHaveBeenCalled();
expect(ctrl.minYear).toBe(2013);
})
});
});

这是 live code

最佳答案

当使用 ngMock 进行单元测试时,您需要同步维护测试流程并手动触发摘要周期以使 promise 实际返回。

例如,您可以通过调用 $rootScope.$digest() 来执行此操作:

it("activate sets min max dates", function() {
createController(true);
$rootScope.$digest();
expect(ctrl).toBeDefined();
expect(service.getMinMaxDates).toHaveBeenCalled();
expect(ctrl.minYear).toBe(2013);
});

演示: http://plnkr.co/edit/vWs1Dx0bjXsdrWCJKWh9?p=preview

关于javascript - 测试调用服务返回 promise 的 Angular Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26868056/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com