AngularJS单元测试错误:范围是未定义 [英] AngularJS unit test error: scope is undefined
问题描述
我有AngularJS单元测试的一个问题。在单元测试的范围是不确定的。但首先源$ C $ C:
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:
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");
});
});
});
错误消息:范围未定义
第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");
});
});
});
错误消息:控制器不是一个函数
我用的业力,如果它取决于此。
I use karma, if it depends on this.
这是从角种子karma.conf。这些是我使用的所有库。我想我并不需要每LIB测试,所以我评论了这一点。如果一切都注释掉,发生同样的错误。
编辑:我使用的每一个LIB是在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 $新的()。
我希望有人能帮助我。
Why the scope is undefined? The scope gets the $rootScope.$new(). I hope someone can help me.
感谢。
PS:我问在Google网上论坛同一个问题 - > https://开头组.google.com /论坛/#!话题/角/ 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屋!