2016-10-24 1 views
3

Erhalten des Fehlers als aufgeführt, während die Prüfung der LoginControllerEinheit Testwinkelregler mit eingespritztem Dienst

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(); 
     }); 
    }); 
}); 

Wie teste ichrichtig??? Mit dem Code, der wie oben geschrieben wird, erhalte ich den folgenden Fehler, während der Unit-Test.

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) 
+0

Ist 'ajaxService' in' myApp.services' Modul? – mojarras

Antwort

2

Sie sind nicht bootstrapping Ihre module.

beforeEach(module('myApp')); 

Und auch überprüfen, ob Sie Ihren Dienst in karma.conf.js

aufgenommen haben
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' 
], 
+0

Warum der 'angular-ui-router.js'? – slackmart

+0

@SlackMart: nur eine Referenzvorlage ... – Thalaivar