gpt4 book ai didi

javascript - 如何编写 Jasmine 测试用例来检查单击事件中的 Toggle 类功能

转载 作者:行者123 更新时间:2023-12-03 05:18:18 25 4
gpt4 key购买 nike

我有一个指令,基本上可以在元素上切换类。它工作正常。但是它的 Jasmine 测试用例出了问题。

//toggling class
fileSearch.directive('toggleClass', function() {
return {
restrict: 'A',
link: function(scope, element, attrs) {
element.bind('click', function() {
element.toggleClass(attrs.toggleClass);
});
}
};

});

Jasmine 测试用例:

describe(
'testing toggle-class directive',
function() {
var $compile, $rootScope;
beforeEach(module('myApp'));

beforeEach(inject(function(_$compile_, _$rootScope_) {
$compile = _$compile_;
$rootScope = _$rootScope_;
}));

it('should set highlight class on report title',
function() {
var element = $compile("<div toggleclass='highlighted'</div>")($rootScope);
element.click();
expect(element).toHaveClass('highlighted');
});

});

它抛出错误,说 undefined 不是“expect(element).toHaveClass('highlighted');”行的构造函数。你能指导我一下吗?我对 Angular 有点陌生。

最佳答案

toHaveClass 不是 Jasmine 的一部分。尝试类似的方法:

expect(element.attr('class')).toEqual('highlighted');

我已经搞砸了your code here .

关于javascript - 如何编写 Jasmine 测试用例来检查单击事件中的 Toggle 类功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41514126/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com