简体   繁体   English

如何使用documet.getElementById和getElementsByClassName为display none css属性执行jasmine测试用例

[英]How to do jasmine test case for display none css property using documet.getElementById and getElementsByClassName

I am new to the jasmine test case I tried to do jasmine test case for selection module after doing this style property is getting undefined 我是茉莉花测试用例的新手我尝试做茉莉花测试用例选择模块后做了这个样式属性变得不确定

 function Selection() {

    }
    Selection.prototype.expandFlightDetails = function() {

        document.getElementsByClassName("flight-details-container").style.display = 'none';
        document.getElementById("expandedFlightDetails").style.display = 'block';
    };
    Selection.prototype.hideFlightDetails = function() {
        document.getElementById("expandedFlightDetails").style.display = 'none';
        document.getElementsByClassName("flight-details-container").style.display = 'block';

    };

My testcase is 我的测试用例是

describe("selection module", function() {
    var selection;

    beforeEach(function () {
        selection= new Selection();

    });
    afterEach(function () {

    });
    it('expand the flight details part ' , function(){
        selection.expandFlightDetails();
        expect(document.body.getElementsByClassName('flight-details-container')[0].style.display).toEqual('none');

    });
    xit('hide the flight details part ', function(){
        selection.hideFlightDetails();
        expect(document.getElementById('expandedFlightDetails').style.display).toEqual('none');

    });
});

After doing this I'm geting and removed code to beforEach 执行此操作后,我正在设置并删除代码以便beforEach

TypeError: Cannot read property 'style' of undefined TypeError:无法读取未定义的属性“样式”

please correct me if I have done wrong 如果我做错了,请纠正我

You have a few errors on this code. 您在此代码上有一些错误。

First in Selection.prototype.expandFlightDetails make sure to get the first result of the array (you forgot the [0] ): Selection.prototype.expandFlightDetails首先确保得到数组的第一个结果(你忘了[0] ):

document.getElementsByClassName("flight-details-container")[0]

Same comment for Selection.prototype.hideFlightDetails Selection.prototype.hideFlightDetails注释相同

Then in your test suite you create a Selection instance named selection but then in both tests you are using a variable called flightselection which is declared nowhere. 然后在您的测试套件中创建一个名为selection的Selection实例,但在两个测试中,您都使用一个名为flightselection的变量,该变量在任何地方都被声明。 Shouldn't it be selection instead? 不应该selection它吗?

Finally your problem seems to be that you try to manipulate 'flight-details-container' in your test, although this element is created on the afterEach callback. 最后你的问题似乎是你试图在你的测试中操纵'flight-details-container' ,虽然这个元素是在afterEach回调上创建的。 afterEach means that this will be executed after each test, so it doesn't exist during the test. afterEach表示这将在每次测试后执行,因此在测试期间不存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM