I have a method defined in an AngularJS controller that gets called during initialization. I want to test it with Jasmine ( "jasmine-core": "^2.3.4", "karma": "^0.12.37" ). I am following some tutorials on the Internet and StackOverflow, 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 () {
After running the test, the error message is:
PhantomJS 1.9.8 (Windows 7 0.0.0) usersAddUserControllerUnitTest should call the getCountryPhoneCodes method for init FAILED
Expected spy getCountryPhoneCodes to have been called.
Obviously, I missed something, but I canβt understand what it is. Any help would be appreciated.