AngularJS 单元测试错误:范围未定义 [英] AngularJS unit test error: scope is undefined

查看:19
本文介绍了AngularJS 单元测试错误:范围未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 AngularJS 单元测试时遇到问题.单元测试中的范围未定义.但首先是源代码:

I have a problem with AngularJS unit testing. The scope in the unit test is undefined. But first the source code:

主要的角度模块:

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

控制器模块:

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

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

And finally the controller to test as a simplified version:

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

现在开始测试.版本 1:

Now the test. Version 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.

我使用业力,如果它取决于这个.

I use karma, if it depends on this.

这是来自 angular-seed 的 karma.conf.这些都是我使用的库.我想我不需要每个库来测试,所以我把它注释掉了.如果所有内容都取消注释,则会发生相同的错误.我使用的每个库现在都在 karma.conf 中.我收到相同的错误消息.

This is the karma.conf from angular-seed. These are all libs that I used. I think I don't need every lib to test, so I commented this out. If everything is uncommented, the same error occurs. edit: Every lib I use is in the karma.conf now. I get the same error messages.

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().我希望有人能帮助我.

Why the scope is undefined? The scope gets the $rootScope.$new(). I hope someone can help me.

谢谢.

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

P.S: I asked the same question on GoogleGroups -> https://groups.google.com/forum/#!topic/angular/4oAEVLbEZT4

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

But obviously nobody there can help me.

推荐答案

试试这个

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

这篇关于AngularJS 单元测试错误:范围未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆