我想通過.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