2014-02-06 27 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(),似乎間諜也被扼殺了。

相關問題