Karma / Jasmine单元测试出错

时间:2016-09-27 15:38:46

标签: angularjs unit-testing karma-jasmine

这是我的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)

    loaded@http://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)

如何解决此错误?

  

未知提供商:$ routeParamsProvider&lt; - $ routeParams&lt; - MainCtrl

1 个答案:

答案 0 :(得分: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');
        });
    });
});