我已经尝试遵循ng-directive-testing repo 的格式来编写我编写的指令。当用户点击一个元素时,该指令基本上呈现一个覆盖。这是指令(简化):
mod.directive('uiCopyLinkDialog', function(){
return {
restrict: 'A',
link: function(scope, element, attrs) {
var $elm = angular.element(element);
element.bind('click', function(event) {
$elm.addClass('test');
});
}
};
});
我正在编写的测试如下所示:
describe('pre-compiled link', function () {
beforeEach(mocks.inject(function($compile, $rootScope) {
scope = $rootScope;
element = angular.element('<span class="foo" ui-copy-link-dialog="url"></span>');
$compile(element)(scope);
scope.$digest();
}));
it("should change the class when clicked", function () {
element.click(); // this returns "'undefined' is not a function"
element[0].click(); // so does this
$(elm).click(); // this uses jquery and doesn't *seem* to fail
waits(500); // hack to see if it was a race condition
expect(elm.className).toContain('test'); // always fails
});
});
您可以在测试中看到我尝试了几种方法来触发click()
链接上的事件,其中大多数都给出了undefined
错误。
谁能告诉我我在这里做错了什么?阅读示例这听起来像是正确的语法,但我的测试跑步者(Karma via Grunt)不想打球。