【发布时间】:2014-10-23 21:29:34
【问题描述】:
我试图在我$rootScope.broadcast() 一个事件时触发的控制器中触发$scope.$on() 方法。我发现这个问题很有用:How can I test events in angular?,但我仍然无法检测到从 $rootScope 向上通过控制器的 $scope 广播的事件。
到目前为止,我已经成功测试了 $broadcast 方法是在相应的 $rootScope 上调用的,但不是 $on 方法是在相应的 $scope 上调用时在 $broadcast 上调用的$rootScope.
我试图在我的测试中直接$rootScope.$broadcast,但我的间谍没有接听该事件。
这是我的控制器:
angular.module('app.admin.controllers.notes', [])
.controller('NotesCtrl', function($scope) {
$scope.$on('resource-loaded', function(event, resource) { // I want to test this
$scope.parentType = resource.type;
$scope.parentId = resource.id;
});
});
这是我的测试:
describe('The notes controller', function() {
beforeEach(module('app.admin.controllers.notes'));
var scope, rootScope, NotesCtrl;
beforeEach(inject(function($controller, $injector, $rootScope) {
rootScope = $rootScope;
scope = $rootScope.$new(); // I've tried this with and without $new()
NotesCtrl = $controller('NotesCtrl', {$scope: scope}); // I've tried explicitly defining $rootScope here
}));
it('should respond to the `resource-loaded` event', function() {
spyOn(scope, '$on');
rootScope.$broadcast('resource-loaded'); // This is what I expect to trigger the `$on` method
expect(scope.$on).toHaveBeenCalled();
});
});
还有here's the plunkr。我已经包含了$broadcast 方法的通过测试以供参考,主要是因为我以相同的方式设置测试。
我已经阅读了很多与 AngularJS 中的测试事件相关的问题,这似乎总是一个范围界定问题。我听说在 Karma 单元测试中,$rootScope 和 $scope 是一回事,但我不太确定这意味着什么。我尝试将$rootScope 和$scope 定义为同一个对象,并在测试期间将$rootScope 显式注入NotesCtrl,但没有什么能让我的测试变绿。
如何让我的NotesCtrl 中的$on 方法为该测试触发?
【问题讨论】:
标签: angularjs unit-testing karma-runner