3

Getting the error as listed while testing the LoginController

LoginController.js

'use strict';

/*Login Module Controllers */

angular.module('loginModule.controller', ['myApp.services', 'ngMd5']).
controller('LoginController', ['$scope', '$location', 'ajaxService', 'userPersistenceService', 'md5', 'toast',
function($scope, $location, ajaxService, userService, md5, toast) {
    $scope.username = '';
    $scope.password = '';
    $scope.loginSuccess = '';
    $scope.login = function() {
        ajaxService.ajaxPostReq({username: $scope.username, password: md5.createHash($scope.password || '')}, 'http://localhost:3000/user/auth', successFunction, errorFunction);
    }
    function successFunction(response) {
        if(response.status == 'success') {
            $scope.loginSuccess = true;
            userService.setUser({userName: response.username, sessionId: response.sessionId});
            $location.path('/home');
            toast.showMessage('Successfully Logged In', response.status);
        } else {
            errorFunction(response);
        }
    }
    function errorFunction(response) {
        $scope.loginSuccess = false;
        toast.showMessage(response.error, response.status);
    }
}]);

LoginController.test.js

describe('Controller: LoginCtrl', function() {
    angular.mock.module('loginModule.controller', []);
    angular.mock.module('myApp.services', []);

    var ctrl, ajaxService, $q;
    beforeEach(inject(function($controller, _$q_, _ajaxService_){
        ctrl = $controller('LoginController');
        $q = _$q_;
        ajaxService = _ajaxService_;
    }));
    describe('Login function', function() {
        beforeEach(function(){
            var deferred = $q.defer();
            spyOn(ajaxService, 'ajaxPostReq').and.returnValue(deferred.promise);
            ctrl.login();
        });

        it('should call ajaxService', function() {
            expect(ajaxService.ajaxPostReq).toHaveBeenCalled();
        });
    });
});

How do I properly test LoginController?? With the code written as above, I am getting the following error, while unit testing.

 Chrome 53.0.2785 (Mac OS X 10.12.0) Controller: LoginCtrl Login function should call ajaxService FAILED
 Error: [$injector:unpr] Unknown provider: ajaxServiceProvider <- ajaxService
 http://errors.angularjs.org/1.5.6/$injector/unpr?p0=ajaxServiceProvider%20%3C-%20ajaxService
 at client/app/lib/angular/angular.js:68:12
 at client/app/lib/angular/angular.js:4501:19
 at Object.getService [as get] (client/app/lib/angular/angular.js:4654:39)
 at client/app/lib/angular/angular.js:4506:45
 at getService (client/app/lib/angular/angular.js:4654:39)
 at injectionArgs (client/app/lib/angular/angular.js:4678:58)
 at Object.invoke (client/app/lib/angular/angular.js:4700:18)
 at Object.workFn (client/app/lib/angular/angular-mocks.js:3071:20)
 Error: Declaration Location
 at window.inject.angular.mock.inject (client/app/lib/angular/angular-mocks.js:3033:25)
 at Suite.<anonymous> (client/test/LoginModule/LoginController.js:6:20)
 at client/test/LoginModule/LoginController.js:1:5
 TypeError: Cannot read property 'defer' of undefined
 at Object.<anonymous> (client/test/LoginModule/LoginController.js:13:34)
 TypeError: Cannot read property 'ajaxPostReq' of undefined
 at Object.<anonymous> (client/test/LoginModule/LoginController.js:19:35)
 Chrome 53.0.2785 (Mac OS X 10.12.0): Executed 6 of 6 (1 FAILED) (0 secs / 0.07 secs)
 Chrome 53.0.2785 (Mac OS X 10.12.0): Executed 6 of 6 (1 FAILED) (0.007 secs / 0.07 secs)  
1
  • Is ajaxService in myApp.services module? Commented Oct 24, 2016 at 20:31

1 Answer 1

2

You are not bootstrapping your module.

beforeEach(module('myApp'));

And also check whether you have included your service in karma.conf.js

files: [

  'app/bower_components/angular/angular.js',
  'app/bower_components/angular-mocks/angular-mocks.js',
  'app/bower_components/angular-ui-router/release/angular-ui-router.js',
  'app/app.js',
  'app/controllers/*.js',
  'app/services/*.js',
  'tests/**/*.js'
],
Sign up to request clarification or add additional context in comments.

1 Comment

Why the angular-ui-router.js?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.