【问题标题】:AngularJS unit test error: scope is undefinedAngularJS单元测试错误:范围未定义
【发布时间】:2014-03-22 14:26:26
【问题描述】:

我对 AngularJS 单元测试有疑问。单元测试中的范围未定义。但首先是源代码:

主要的角度模块:

var app = angular.module('App', [ 
    'AppCtrl',
    'AppServices',
    'AppDirectives',    
    'ngGrid',
    'ui.bootstrap',
    'angular-growl',
    'ngAnimate'
]);

控制器模块:

var appCtrl = angular.module('AppCtrl', []);

最后将控制器作为简化版本进行测试:

appCtrl.controller('SignalListCtrl', ['$scope',  
    function($scope) { 
        $scope.name = "World";
}]);

现在是测试。版本 1:

    describe("App", function () {
        beforeEach(module("App"));

        describe("SignalListCtrl", function () {
            var scope, controller;

            beforeEach(inject(function ($rootScope, $controller) {
                scope = $rootScope.$new();
                controller = $controller("SignalListCtrl", {$scope: scope});
                //controller = $controller;
                scope.$digest();
            }));

            it("should print World", function () {
                //controller("SignalListCtrl", {$scope: scope});
                expect(scope.name).toBe("World");
            });
        });
    });

错误信息:scope is undefined

第 2 版:

    describe("App", function () {
        beforeEach(module("App"));

        describe("SignalListCtrl", function () {
            var scope, controller;

            beforeEach(inject(function ($rootScope, $controller) {
                scope = $rootScope.$new();
                //controller = $controller("SignalListCtrl", {$scope: scope});
                controller = $controller;
                scope.$digest();
            }));

            it("should print World", function () {
                controller("SignalListCtrl", {$scope: scope});
                expect(scope.name).toBe("World");
            });
        });
    });

错误信息:controller is not a function

我使用业力,如果它取决于此。

这是来自 angular-seed 的 karma.conf。这些都是我使用的库。我认为我不需要每个库都进行测试,所以我对此进行了评论。如果所有内容都未注释,则会发生相同的错误。 编辑:我使用的每个库现在都在 karma.conf 中。我收到相同的错误消息。

module.exports = function(config){
    config.set({
    basePath : '../',

    files : [        
        'app/lib/jquery/jquery.js',
        'app/lib/jquery/jquery-ui-dialog.js',
        'app/lib/jquery/jquery-dialog.js',    
        'app/lib/angular/angular.js',
        'app/lib/angular/angular-*.js',
        'app/lib/angular/growl/*.js',          
        'app/lib/bootstrap/*.js',
        'app/lib/highcharts/highcharts.js',   
        'app/lib/highcharts/export.js',  
        'app/lib/xml2json/*.js', 
        'app/js/*.js',
        'test/lib/angular/angular-mocks.js',
        'test/unit/*.js'
    ],

    exclude : [
    ],

    autoWatch : true,

    frameworks: ['jasmine'],

    browsers : ['Firefox'],

    plugins : [
            'karma-junit-reporter',
            'karma-chrome-launcher',
            'karma-firefox-launcher',
            'karma-jasmine'
            ],

    junitReporter : {
      outputFile: 'test_out/unit.xml',
      suite: 'unit'
    }

})};

为什么范围未定义?范围获得 $rootScope.$new()。 我希望有人可以帮助我。

谢谢。

P.S:我在 GoogleGroups 上问过同样的问题 -> https://groups.google.com/forum/#!topic/angular/4oAEVLbEZT4

但显然没有人可以帮助我。

【问题讨论】:

  • 您可以尝试将scope.$digest() 添加为beforeEach 的最后一行吗?
  • 添加了 scope.$digest()。错误信息是一样的。
  • 您正在尝试在测试中加载“应用”模块。这个App模块依赖于ngGrid、ui.bootstrap等,但是定义这些模块的JS文件不包含在karma文件中。
  • 我发现了错误。使用 Karma 中的 logLevel LOG_DEBUG(我以前不知道),我看到了找不到 highcharts 的消息。因为在 karma.conf 中的“app/lib/highcharts/*.js”行,在“highcharts.js”之前按字母顺序包含“export.js”。现在它工作正常。谢谢大家。
  • 您不应编辑问题中的代码来修复它。最好回答你自己的问题来分享和解释,以供将来遇到同样问题的开发者使用。

标签: angularjs unit-testing controller scope undefined


【解决方案1】:

试试这个

describe("App", function () {
    beforeEach(module("App"));

    describe("SignalListCtrl", function () {
        var scope ,ctrl;

        beforeEach(inject(function ($rootScope, $controller) {

              scope = $rootScope.$new();
            ctrl= $controller("SignalListCtrl", {$scope: scope});

        }));

        it("should print World", function () {

            expect(ctrl.name).toBe("World");
        });
    });
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-06
    • 2015-06-29
    • 2019-06-19
    • 1970-01-01
    • 2015-06-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多