【问题标题】:How to write jasmine test case for checking Toggle class functionality inside click event如何编写 jasmine 测试用例来检查点击事件中的切换类功能
【发布时间】:2017-05-21 17:07:30
【问题描述】:

我有一个指令,基本上是在元素上切换类。它工作正常。但是它的 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');
                });

    });

它的抛出错误是在“expect(element).toHaveClass('highlighted');”行说 undefined is not a constructor 你能指导我吗?我对 Angular 有点陌生。

【问题讨论】:

    标签: javascript angularjs karma-jasmine


    【解决方案1】:

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

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

    我已经刷了your code here

    【讨论】:

    • 谢谢哥们!!!。 bdw 通过属性检查类不起作用似乎我使用了 hasClass !期望(element.hasClass('highlighted')).toBe(true);
    • @BharatSewani 检查类属性有效。测试成功运行。请重新检查 plunkr。你用的是什么 Jasmine 版本?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-29
    • 2020-11-28
    • 1970-01-01
    • 1970-01-01
    • 2015-07-06
    • 2021-07-11
    相关资源
    最近更新 更多