2016-09-27 170 views
1

這是我的AngularJS代碼:錯誤噶/茉莉花單元測試

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