2016-09-27 4 views
1

Это мой AngularJS код:Ошибка в Karma/Жасмин тест Unit

angular.module('myapp', []) 
    .controller('MainCtrl', function($scope, $http, $rootScope, $routeParams) { 
     $scope.name = "Girish"; 
     $scope.sayHello = function() { 
      $scope.greeting = "Hello " + $scope.name; 
     }; 
     $scope.commonUrl = "/"; 
     $rootScope.$watch('eng', function() { 
      $scope.currentLang = $rootScope.CURRENT_LANGUAGE; 
     }); 
     $scope.homePageFirstSlider = function() { 
      $scope.anun = "Nar"; 
      $http({ 
       method: 'GET', 
       url: "/" + "eng" + '/api/getslideritems/main' 
      }).then(function successCallback(response) { 
        $scope.Data = response.data; 
        $scope.loadCss('mainCarousel'); 
       }, 
       function errorCallback(response) {}); 
     }; 
    }); 

Это тестовый файл:

'use strict'; 
describe('myapp', function() { 
    beforeEach(module('myapp')); 
    var $controller; 

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

    describe('$scope.sayHello', function() { 
     it('same tests', function() { 
      var $scope = {}; 
      var controller = $controller('MainCtrl', { $scope: $scope }); 
      $scope.name = 'Girish'; 
      $scope.sayHello(); 
      expect($scope.greeting).toEqual('Hello Girish'); 
     }); 
    }); 
}); 

После бегаю karma.conf.js файл У меня есть эта ошибка:

PhantomJS 2.1.1 (Linux 0.0.0) myapp $scope.sayHello same tests FAILED 
    ***Error: [$injector:unpr] Unknown provider: $routeParamsProvider <- $routeParams <- MainCtrl*** 
    http://errors.angularjs.org/1.5.0/$injector/unpr?p0=%24routeParamsProvider%20%3C-%20%24routeParams%20%3C-%20MainCtrl in /var/www/html/famboxv2/public/bower_components/angular/angular.js (line 4397) 

    [email protected]://localhost:9882/context.js:151:17 
PhantomJS 2.1.1 (Linux 0.0.0): Executed 1 of 1 (1 FAILED) ERROR (0.025 secs/0.004 secs) 
Chromium 45.0.2454 (Ubuntu 0.0.0): Executed 1 of 1 (1 FAILED) ERROR (0.008 secs/0.005 secs) 

Как я могу решить эту ошибку?

Unknown provider: $routeParamsProvider <- $routeParams <- MainCtrl

ответ

0

$routeParams служба является частью ngRoute модуля. Поэтому вам придется включить его в зависимость от вашего модуля.

Вот как:

angular.module('myapp', ['ngRoute']) 

Кроме того, вы должны быть насмешливым контроллер в первом beforeEach блоке вместо насмешливый его внутри конкретного теста, так что он может повторно использоваться во всех ваших тестов.

И ваши тесты по-прежнему будут терпеть неудачу, потому что вы вводите пустой объект для $scope вместо создания новой области с помощью функции $rootScope$new. Поэтому вы должны внести следующие изменения, чтобы заставить их пройти:

describe('myapp', function() { 
    beforeEach(module('myapp')); 

    //Replace this 
    beforeEach(inject(function($controller, _$rootScope_){ 
    //With this 
    // beforeEach(inject(function($controller, _$rootScope_, _$routeParams_, _$http_){ 

     //Uncomment the following comments if needed for writing tests for the $watch function. 
     // $rootScope = _$rootScope_; 
     $scope = _$rootScope_.$new(); 
     // $http = _$http_; 
     // $routeParams = _$routeParams_; 
     controller = $controller('MainCtrl', { 
      $scope: $scope, 
      // $http: $http, 
      // $rootScope: $rootScope, 
      // $routeParams: $routeParams 
     }); 
    })); 

    describe('$scope.sayHello', function() { 
     it('same tests', function() { 
      $scope.name = 'Girish'; 
      $scope.sayHello(); 
      expect($scope.greeting).toEqual('Hello Girish'); 
     }); 
    }); 
}); 
Смежные вопросы