【问题标题】:Jasmine test help needed afterEach茉莉花测试后需要帮助
【发布时间】:2018-10-03 00:40:48
【问题描述】:

我有一个使用 jQuery 构建的逻辑,它使用两个 each 循环。我不确定如何为它编写茉莉花测试。

这是我的代码/逻辑:

<div id="mj">
  <div class="commonclass">
    Item 1
  </div>
  <div class="commonclass">
    Item 2
  </div>
  <div class="commonclass">
    Item 3
  </div>
</div>

jQuery:

var filterArray = ['Item', 'Block', 'Item 3'];

$.each($('.commonclass'), function(i, v) {
  var matching = false;
  $.each(filterArray, function(j, w) {
    if ($(v).text().trim() == w)
      matching = true;
  });
  if (matching)
    $(v).remove();
});

逻辑:

来自 div 元素的文本不应与 filterarray 值匹配。如果匹配,则应删除该元素。

这是我的茉莉花测试代码。 (添加部分)

describe('Delete matching element', function() {
        beforeAll(function() {
            elementsEl = element(SelectorData.selectors.elementSelector);
            elementValueEl = element(SelectorData.selectors.elementTitleSelector);
            bodyDocumentEl = element(SelectorData.selectors.body);
            bodyDocumentValues = SelectorData.values;
        });

        afterEach( function() {

            if (elementValueEl.value == bodyDocumentValues.value) {
                document.body.removeChild(elementsEl);
            }

        });

        it('delete the element', function() {
            expect(elementsEl.isPresent()).toBe(false);

        });
    });

让我知道我在这里做错了什么。 JSfiddle: https://jsfiddle.net/dmahendranme/g1L2Lzm1/

【问题讨论】:

  • 没有 jQuery 更简单:jsfiddle.net/g1L2Lzm1/1
  • 不相关但 google jasmine-jquery - 这是一个小型库,可帮助您的代码保持同步,因此在测试中也可以使用 jquery 访问 jquery 对象 - 仅供参考

标签: javascript jquery unit-testing jasmine jasmine-jquery


【解决方案1】:

试试这个,

$(function(){    
    var filterArray = ['Item', 'Block', 'Item 3'];
    $('.commonclass').each(function(k,v){        
        if($.inArray($.trim($(v).text()), filterArray) > 0){
            $(this).remove()
        }
    });
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多