代码之家  ›  专栏  ›  技术社区  ›  Cameron

茉莉测试元素是否消失

  •  0
  • Cameron  · 技术社区  · 8 年前

    我正在为以下两个函数编写Jasmine测试:

    var showSpinner = function () {
        $('#spinner').remove();
        $('<div id="spinner"><i class="fa fa-spinner fa-pulse fa-3x fa-fw"></i></div>')
            .appendTo('body')
            .hide()
            .fadeIn();
    };
    
    var hideSpinner = function () {
        $('#spinner').fadeOut(function () {
            $(this).remove();
        });
    };
    

    我的测试如下:

    it('show spinner', function () {
        showSpinner();
        expect($('#spinner').length).toEqual(1);
    });
    
    it('hide spinner', function () {
        hideSpinner();
        expect($('#spinner').length).toEqual(0);
    });
    

    第一个测试很好。然而,由于 fadeOut hideSpinner 第二次测试失败。

    it('hide spinner', function () {
        hideSpinner();
        setTimeout(function () {
            expect($('#spinner').length).toEqual(0);
        }, 100);
    });
    

    但这会导致测试通过,但会警告大家不要抱有任何期望。。。

    我如何测试这个?

    1 回复  |  直到 8 年前
        1
  •  1
  •   André Dion    8 年前

    你应该了解 Jasmine's Asynchronous Support :

    it('hide spinner', function(done) {
        hideSpinner();
        setTimeout(function() {
            expect($('#spinner').length).toEqual(0);
            done();
        }, 400);
    });
    

    400 ms是的默认超时 $.fadeOut .