簡體   English   中英

帶注入服務的單元測試角度控制器

[英]Unit test angular controller with injected service

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

我如何正確測試LoginController 使用上面編寫的代碼,我在單元測試時收到以下錯誤。

 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)  

你沒有bootstrapping你的module

beforeEach(module('myApp'));

並檢查您是否已將服務包含在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'
],

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM