I am currently in the process of testing an angular controller that relies on a service with a method that returns a promise. I have created a jasmine spy object to simulate the service and its promise-returning method. However, my mock promise is not returning the expected result for some reason.
Below is the code for my controller and service:
(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;
}
}
}
})();
Here is the test code:
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);
})
});
});
Check out the live code here.