設定
ここでは、要素を作成、コンパイル、リンク、および追加してから、イベント スパイを設定しています。
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
。