簡體   English   中英

$ index和量角器的AngularJS ng-repeat跟蹤重復

[英]AngularJS ng-repeat track by $index and Protractor with Duplicates

我有一個字符串數組,我需要允許重復。

vm.list = ["item","item","item","item","item"]

這是用html處理的

<ul class="listItems>
    <li ng-repeat="item in ctrl.list track by $index"></li>
</ul>

這在DOM中顯示正常,沒有問題,但是當我嘗試量角器測試ng-repeat時遇到了問題,因為我無法對其進行單元測試。

所以我的測試有點像。

Then("List items should contain {int} items.", function(listLength){
    return element(by.css(".listItems").all(by.repeater('item in ctrl.list track by $index')).then(function(list){
        return expect(list.length).equal(listLength);
    });
});

我運行測試,但失敗,期望0為5。但是,如果我使它們全部唯一,則可以正常工作,如何解決此問題?

我相信這應該為您工作:

Then("List items should contain {int} items.", function(listLength){
    let list = element(by.css(".listItems").all(by.repeater('item in ctrl.list'));
    expect(list.count()).toBe(listLength);
});

來源: http : //www.protractortest.org/#/api? view= ElementArrayFinder.prototype.count

編輯:對不起,我誤解你的問題,看來track by不與數組中的重復值工作。 請參閱: https : //docs.angularjs.org/api/ng/directive/ngRepeat 您可以替換自己的跟蹤功能。

這是一個類似問題的示例: https : //stackoverflow.com/a/28232640/3111958

第2次修改:多看了幾遍之后,量角器不再按比例使用軌跡。 我更新了代碼塊以反映這一點。 查看此問題: https : //stackoverflow.com/a/37535098/3111958

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM