虽然我的测试是正确的,但我不知道这些错误意味着什么,我已经尝试过几件事了。
Can't find variable: $rootScope
Error: Injector already created, can not register a module!spec.js
describe('test broadcast', function () {
var $controller;
beforeEach(function() {
module('test');
inject(function (_$rootScope_, _$controller_) {
$rootScope = _$rootScope_;
spyOn($rootScope, '$broadcast');
// Notice how inject $controller here.
$controller = _$controller_;
});
});
it("should broadcast something", function ($rootScope) {
$controller('myCtrl', {
// Pass in the $rootScope dependency.
$rootScope: $rootScope.$new()
})
// Here we actually run the controller.
expect($rootScope.$broadcast).toHaveBeenCalledWith('update');
//someObj = { data: testData};
//expect($rootScope.$broadcast).toHaveBeenCalledWith('update', someObj);
});
})控制器
(function () {
var test= angular.module('test');
test.controller('myCtrl',
function($rootScope, $scope, $resource, $location, $route, $routeParams, $log, catalogData) {
$log.debug("myCtrl");
$log.debug(myCtrl);
$rootScope.$broadcast("update", {
data: testData
});
}); // catalogCtrl
})();发布于 2016-02-02 13:38:41
编辑2:
您不能在您的$rootScope函数中访问it,因为它不在当前的javascript scope中(不是棱角的$scope,不要感到困惑)。
您需要定义它与您的控制器在顶部。
var $controller, $rootScope并将$rootScope从您的it函数中删除,这样您就不会覆盖它。
// Notice there is no $rootScope parameter.
it("should broadcast something", function () {
//Code
}您还必须传递其他依赖项。在与OP讨论之后,整个代码应该如下所示:
describe('test broadcast', function () {
var $controller, $rootScope;
beforeEach(function() {
module('test');
inject(function (_$rootScope_, _ $controller_) {
$rootScope = _$rootScope_;
spyOn($rootScope, '$broadcast');
$controller = _$controller_;
});
});
it("should broadcast something", function () {
$controller('myCtrl', {
$scope: $rootScope.$new(),
catalogData: {}
})
expect($rootScope.$broadcast).toHaveBeenCalledWith('update', {catalog:{}})});
})编辑1:
您正在传递$scope依赖项。$broadcast在$rootScope上被调用,所以您需要传递它。如下所示:
var testScope = $rootScope.$new()
$controller('myCtrl', {
// Pass in the $rootScope dependency.
$rootScope: testScope
}原始帖子(万一它对任何人仍然有用)
实际上,您并没有在测试套件中的任何地方调用您的控制器。
你需要的东西是
var $controller
beforeEach(inject(function (_$rootScope_, _$controller_) {
$rootScope = _$rootScope_;
spyOn($rootScope, '$broadcast');
// Notice how inject $controller here.
$controller = _$controller_;
}));然后在测试中初始化它:
it("should broadcast something", function () {
// Here we actually run the controller.
$controller('myCtrl', {
// Pass in the $rootScope dependency.
$rootScope: $rootScope.$new()
}
expect($rootScope.$broadcast).toHaveBeenCalledWith('catalogUpdate');
someObj = { catalog: catalogData};
expect($rootScope.$broadcast).toHaveBeenCalledWith('catalogUpdate', someObj);
}); 这将删除有关$rootScope.broadcast未被调用的错误。
看看这里的“测试控制器”部分:https://docs.angularjs.org/guide/controller
至于不能注册一个模块,这通常发生在您有一个inject()之前的beforeEach(module('abc'))。正如错误所述,在调用inject之后,您无法注册另一个模块。
发布于 2016-02-02 13:15:11
定义了一个名为rootScope的变量,而不是$rootScope --更改定义:
rootScope.$apply();虽然我个人喜欢这样定义它们:
var $rootScope;
beforeEach(inject(function(_$rootScope_) {
$rootScope = _$rootScope_;
}));https://stackoverflow.com/questions/35154343
复制相似问题