2014-02-06 33 views
0

设置

在这里我创建,编译,链接和附加元素,然后设置事件间谍。如何在dom的角度元素上使用事件间谍?

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它的内部时,将触发我模拟点击事件。

回答

0

e.stopImmediatePropagation();

当然,我立即意识到罪魁祸首。

所以,毫不奇怪的是,jasmine-jquery将不会接受其处理程序调用e.stopImmediatePropagation()的事件。

Jasmine-jquery确实提供了一种方法.toHaveBeenStopped()但由于我使用stopImmediatePropagation(),似乎间谍也被扼杀了。

相关问题