我想通过.on在我的指令中测试$广播(从控制器)的接收器。在指令预期间谍测试.on得到函数
指令:
describe('<-- myDirective Spec ------>', function() {
var scope, $compile, element, $httpBackend, rootScope;
beforeEach(angular.mock.module('myApp'));
beforeEach(inject(function (_$rootScope_, _$compile_, _$httpBackend_) {
scope = _$rootScope_.$new();
$compile = _$compile_;
$httpBackend = _$httpBackend_;
rootScope = _$rootScope_;
var html = '<my-directive></my-directive>';
element = $compile(angular.element(html))(scope);
spyOn(scope, '$broadcast').and.callThrough();
scope.$digest();
}));
it('should be defined', function() {
expect(element).toBeDefined();
});
it('should broadcast ', function() {
scope.$broadcast('sendEmail');
expect(scope.$on).toHaveBeenCalledWith('sendEmail', jasmine.any(Function));
});
});
通过上述,我得到错误:
Expected a spy, but got Function.
更换
您是在'$范围从事间谍活动。$ broadcast',不'$ on'。 – Lee