Setup
在这里,我正在创建,编译,链接和附加元素,然后设置事件间谍。
describe('Click Confirm', function() {
var eventSpy, element, scope;
beforeEach(module('app'));
beforeEach(inject(function ($location, $rootScope, $compile) {
var el, linkFn;
scope = $rootScope.$new();
el = angular.element('<a id="testClickConfirm" href="" ng-click="deleteFn()" click-confirm="Test confirmation message">Delete</a>');
// The $compile method returns the directive's link function
linkFn = $compile(el);
// The link function returns the resulting DOM object
element = linkFn(scope);
$('body').append(element);
eventSpy = spyOnEvent('#testClickConfirm', 'click');
});
初始测试
这些传递并确认元素存在,在 DOM 中并且可见。
it('should have set up correctly', function() {
expect(element).toExist();
expect(element).toBeInDOM();
expect(element).toBeVisible();
expect(el).toExist();
expect(el).toBeInDOM();
expect(el).toBeVisible();
expect($('#testClickConfirm')).toExist();
expect($('#testClickConfirm')).toBeInDOM();
expect($('#testClickConfirm')).toBeVisible();
});
事件间谍测试
it('should have set up correctly', function() {
element.click();
expect(eventSpy).toHaveBeenTriggered(); // <- FAILS
expect('click').toHaveBeenTriggeredOn('#testClickConfirm'); // <- FAILS
});
});
我做错了什么?为什么那些事件间谍说点击没有被触发。
附加信息
我在正在测试的指令中设置了一个element.bind('click', ...
,并在其中添加了一个console.log
,当我模拟单击事件时会触发。
e.stopImmediatePropagation();
当然,我立即意识到罪魁祸首。
因此,不出所料,jasmine-jquery 不会接收其处理程序调用e.stopImmediatePropagation()
的事件。
Jasmine-jquery确实提供了一种方法.toHaveBeenStopped()
但是由于我正在使用stopImmediatePropagation()
因此间谍似乎也被扼杀了。