I'm trying to test a controller which uses the controllerAs syntax. My problem is that when I try to test a function on the controller, I get "Expected undefined to be defined."
Controller (shortened version)
(function (angular) {
'use strict';
/* @ngInject */
function PreflightCtrl($state, $window, $timeout, $stateParams, toastr, accountService, servicesService, geoService, visitService, localStorageService, Notifications, service, UTILS) {
/* Exported Vars */
var vm = this;
/* Exported functions */
vm.verifyDob = verifyDob;
function verifyDob() {
if (!vm.form.dob || (vm.form.dob.length !== 8 && vm.form.dob.length !== 10)){
return;
}
if(vm.form.dob.length === 8){
var lastTwoDigits = vm.form.dob.substr(vm.form.dob.length-2);
if(lastTwoDigits === '19'){
return;
}
}
var dob = new Date(vm.form.dob);
vm.verifyingDob = true;
accountService.verifyDOB(dob)
.success(function(data){
vm.genderDisabled = false;
vm.dobError = false;
$timeout(function() {
dobInput.blur();
});
})
.error(function (status) {
vm.genderDisabled = true;
vm.dobError = true;
})
.finally(function () {
vm.verifyingDob = false;
});
}
}
angular
.module('app')
.controller('PreflightCtrl', PreflightCtrl);
}(angular));
Test
describe('PreflightCtrl', function(){
var PreflightCtrl, $state, $window, $timeout, $stateParams, toastr, accountService, servicesService, geoService, visitService, localStorageService, Notifications, service, UTILS, mockAccountService, mockServicesService;
beforeEach(module('app'));
beforeEach(inject(function($controller, _$state_, _$window_, _$timeout_, _$stateParams_, _toastr_, _accountService_, _servicesService_, _geoService_, _visitService_, _localStorageService_, _Notifications_, _UTILS_){
//mock services used in PreflightCtrl
mockAccountService = {
verifyDOB: function verifyDOB(dob){
return {
success: function(callback){
callback(dob);
}
}
},
isPermUser: function isPermUser(){
return {
success: function(callback){
callback(true);
}
}
},
profile: {
gender: 'male'
}
};
mockServicesService = {
isGenderAllowed: function isGenderAllowed(serviceCode, gender){
return true;
}
}
//prepare for dependency injection
$state = _$state_;
$window = _$window_;
$timeout = _$timeout_;
$stateParams = _$stateParams_;
toastr = _toastr_;
accountService = mockAccountService;
servicesService = mockServicesService;
geoService = _geoService_;
visitService = _visitService_;
localStorageService = _localStorageService_;
Notifications = _Notifications_;
service = {"id": 3, "code": "HL", "flags": {"multi_medicine": false}, "genders": ["male"], "name": "Hair Loss", "product": {"count": 3}, "visible": 1};
UTILS = _UTILS_;
//spy on the mocked services
spyOn(accountService, 'verifyDOB').and.callThrough();
spyOn(servicesService, 'isGenderAllowed').and.callThrough();
//create the controller
PreflightCtrl = $controller('PreflightCtrl', {
$state: $state,
$window: $window,
$timeout: $timeout,
$stateParams: $stateParams,
toastr: toastr,
accountService: accountService,
servicesService: servicesService,
geoService: geoService,
visitService: visitService,
localStorageService: localStorageService,
Notifications: Notifications,
service: service,
UTILS: UTILS
});
}));
it('should have a defined controller', function(){
expect(PreflightCtrl).toBeDefined();
});
it('should have a defined function called verifyDob', function(){
console.log(PreflightCtrl);
expect(PreflightCtrl.verifyDob).toBeDefined();
});
it('should verify a DOB', function(){
PreflightCtrl.form.dob = '01/01/1990';
PreflightCtrl.verifyDob();
expect(PreflightCtrl.verifyingDob).toBe(false);
});
});
When I run the test, this is the output that I get:
Running "karma:unit" (karma) task PhantomJS 1.9.8 (Mac OS X 0.0.0) LOG: 'WARNING: Tried to load angular more than once.'
LOG: Promise{$$state: Object{status: 2, value: Error{message: ...}}} PhantomJS 1.9.8 (Mac OS X 0.0.0) PreflightCtrl should have a defined function called verifyDob FAILED Expected undefined to be defined. at /Users/tracy/Projects/LemonaidClinic/src/views/preflight/preflight.controller.spec.js:80 PhantomJS 1.9.8 (Mac OS X 0.0.0) PreflightCtrl should verify a DOB FAILED TypeError: 'undefined' is not an object (evaluating 'PreflightCtrl.form.dob = '01/01/1990'') at /Users/tracy/Projects/LemonaidClinic/src/views/preflight/preflight.controller.spec.js:85 PhantomJS 1.9.8 (Mac OS X 0.0.0): Executed 15 of 15 (2 FAILED) (0.006 secs / 0.204 secs) Warning: Task "karma:unit" failed. Use --force to continue.
Aborted due to warnings.
Line 80 is this line: expect(PreflightCtrl.verifyDob).toBeDefined();
and line 85 is this: PreflightCtrl.form.dob = '01/01/1990';
and you can see the first line is the log of the controller, which returns a promise which means none of the functions or variables are on the controller that need to be there.
I'm also unsure why Angular is trying to load more than once, but it doesn't seem to be affecting any other tests.
What am I doing wrong?
Thank you in advance.
Aucun commentaire:
Enregistrer un commentaire