I have a method defined in AngularJS controller which is called on initialization. I want to test it using Jasmine ("jasmine-core": "^2.3.4", "karma": "^0.12.37"
). I follow some tutorials on the Internet and StackOverflow questions, but I cannot find the right answer. Please take a look at this code:
Controller usersAddUserController
:
(function () { 'use strict'; angular.module('app.users.addUser') .controller('usersAddUserController', ['$scope', 'usersAddUserService', function ($scope, usersAddUserService) { usersAddUserService.getCountryPhoneCodes().then(function (phoneCodes) { $scope.phoneCodes = phoneCodes; }); }]); }());
Jasmine test:
(function () { 'use strict'; describe('usersAddUserControllerUnitTest', function () { var scope, deferred, objectUnderTest, mockedAddUserService; beforeEach(module('app')); beforeEach(inject(function ($rootScope, $q, $controller) { scope = $rootScope.$new(); function emptyPromise() { deferred = $q.defer(); return deferred.promise; } mockedAddUserService = { getCountryPhoneCodes: emptyPromise }; objectUnderTest = $controller('usersAddUserController', { $scope: scope, usersAddUserService: mockedAddUserService }); })); it('should call getCountryPhoneCodes method on init', function () { //when spyOn(mockedAddUserService, 'getCountryPhoneCodes').and.callThrough(); deferred.resolve(); scope.$root.$digest(); //then expect(mockedAddUserService.getCountryPhoneCodes).toHaveBeenCalled(); }); }); }());
After running the tests, the error message is:
PhantomJS 1.9.8 (Windows 7 0.0.0) usersAddUserControllerUnitTest should call getCountryPhoneCodes method on init FAILED
Expected spy getCountryPhoneCodes to have been called.
I obviously missing something, but I cannot figure out what it is. Any help will be appreciated.