2015-02-03 6 views
0

컨트롤러 단위 테스트 방법에 대한 각도 튜토리얼을 따르고 있습니다. 나는 테스트를 위해 카르마와 재스민을 사용하고 있습니다. 테스트를 실행할 때단위 Angular.js에서 컨트롤러 테스트

Error: [ng:areq] Argument 'testController' is not a function, got undefined 

test.js에서 testController를로드하는 방법에 대한 아이디어가 있습니까? 아래 코드. 감사!

<!-- app.js --> 

var simulatorApp = angular.module('simulatorApp', ['ngRoute', 'ngResource', 'ngCookies'], 

simulatorApp.config(['$routeProvider', function($routeProvider) { 
    $routeProvider 
     .when('/', {templateUrl: '/angular/views/home.html', controller: homeController}) 
     .when('/test', {templateUrl: '/angular/views/test.html', controller: testController}) 
     .otherwise({redirectTo: '/'}); 
}]); 

<!-- testController.js --> 

function testController($scope) { 
    $scope.password = ''; 
    $scope.grade = function() { 
     var size = $scope.password.length; 
     if (size > 8) { 
      $scope.strength = 'strong'; 
     } else if (size > 3) { 
      $scope.strength = 'medium'; 
     } else { 
      $scope.strength = 'weak'; 
     } 
    }; 
} 

<!-- test.js --> 
describe('testController', function() { 
    beforeEach(module('simulatorApp')); 

    var $controller; 

    beforeEach(inject(function (_$controller_) { 
     // The injector unwraps the underscores (_) from around the parameter names when matching 
     $controller = _$controller_; 
    })); 

    describe('$scope.grade', function() { 
     it('sets the strength to "strong" if the password length is >8 chars', function() { 
      var $scope = {}; 
      var controller = $controller('testController', {$scope: $scope}); 
      $scope.password = 'longerthaneightchars'; 
      $scope.grade(); 
      expect($scope.strength).toEqual('strong'); 
     }); 
    }); 
}) 

답변

1

는 다음 작업 plunkr의 모듈 컨트롤러를 정의하지 않았다.

Narek Mamikonyan으로 회원님의 컨트롤러를 모듈에 등록하지 않았습니다. 당신이 당신의 자바 스크립트 파일에 축약이있는 경우 또한 homeController

simulatorApp.controller('homeController', homeController); 

function homeController($scope) { 
    ... 
}; 

, 당신이 시도하고이 같은이 같은 컨트롤러 뭔가를 선언해야와 가능성

simulatorApp.controller('testController', testController); 

function testController($scope) { 
    ... 
}; 

같은 문제는 폭발하지 않습니다

simulatorApp.controller('testController', ['$scope', testController]); 

그렇지 않은 경우 $은 축소 된 후에는 동일하지 않으므로 앱이 작동하지 않습니다.

0

당신은

simulatorApp.controller('testController', function testController($scope) { 
    $scope.password = ''; 
    $scope.grade = function() { 
     var size = $scope.password.length; 
     if (size > 8) { 
      $scope.strength = 'strong'; 
     } else if (size > 3) { 
      $scope.strength = 'medium'; 
     } else { 
      $scope.strength = 'weak'; 
     } 
    }; 
}) 
+0

$ routeProvider에서 컨트롤러를 문자열로 참조해야했습니다. 감사! –

관련 문제