2015-01-07 1 views
0

저는 AngularJS 컨트롤러를 테스트하기 위해 Jasmine을 사용하고 있으며 모듈에 $ routeProvider를 삽입하는 방법을 찾기 위해 고심하고 있습니다. 여기 재스민을 사용하여 AngularJS 테스트를 위해 routerProvider를 삽입하십시오.

var app = angular.module('testApp', []); 

app.config(['$routeProvider', 
    function ($routeProvider) { 
     $routeProvider. 
      when('/smth', { 
       templateUrl: 'smth.html', 
       controller: 'testController' 
      }); 
    }]); 

app.controller('testController', ['$scope', function ($scope) { 
    $scope.data = 'GG'; 
}]); 

내 테스트입니다 : 여기

내 컨트롤러의

describe('Test Suite', function() { 
    var myScope, ctrl; 

    beforeEach(module('testApp')); 

    beforeEach(inject(function ($controller, $rootScope) { 
     myScope = $rootScope.$new(); 
     ctrl = $controller('testController', { 
      $scope: myScope 
     }); 
    })); 

    it('data is GG', function() { 
     expect(myScope.data).toEqual('GG'); 
    }); 
}); 

내가 그것을 실행하려고하면, 나는 다음과 같은 오류가 나타납니다

Error: [$injector:modulerr] Failed to instantiate module testApp due to: 
Error: [$injector:unpr] Unknown provider: $routeProvider 

는하지만 시도하는 경우를 다시 실행하려면 - 이걸 :

TypeError: 'undefined' is not an object (evaluating 'myScope.data') 

테스트를 다시 실행하면 오류가 계속 발생합니다. Jasmine 테스트를 실행하기 위해 Visual Studio 2013 및 Resharper 8을 사용하고 있습니다.

답변

0

먼저 당신이 각-route.min.js을 포함했는지 확인이

var app = angular.module('testApp', ['ngRoute']); 
app.config(['$routeProvider', function ($routeProvider) { 
    $routeProvider. 
     when('/smth', { 
      templateUrl: 'smth.html', 
      controller: 'testController' 
     }); 
}]); 

app.controller('testController', ['$scope', function ($scope) { 
    $scope.data = 'GG'; 
}]); 
0

처럼 모듈로 프로젝트에 각 라우트 정자 구성 요소를 추가 한 후 ngRoute를 주입. 이 모듈은 angularJs 라이브러리와 분리되어 있으므로 별도로 포함해야합니다.

다음 모듈을 ngRoute 에 추가했는지 확인하십시오. angular.module ('testApp', [ 'ngRoute']);

다음 테스트에서 당신은뿐만 아니라 당신의 검사 결과에 $ 노선 때문에 사용 가능한을 주입해야합니다

관련 문제