我如何在我的量angular器testing中使用lodash _.find?

我想使用Lodash findfunction,使我的量angular器testing更健壮。

代替

  element.all (by.repeater ('topic in topics')).then (function (topics) { expect (topics[1].element (by.binding ('topic.name')).getText()).toEqual ('Maths'); expect (topics[1].element (by.binding ('topic.description')).getText()).toEqual ('2 + 2 = 4'); }); 

就像是

  element.all (by.repeater ('topic in topics')).then (function (topics) { var mathsTopic = _.find (topics, 'topic.name', 'Maths'); expect (mathsTopic.element (by.binding ('topic.description')).getText()).toEqual ('2 + 2 = 4'); }); 

我的推理是,如果页面中的项目顺序发生变化,testing不会中断,因为它仍然可以find正在查找的数据的元素。

       

网上收集的解决方案 "我如何在我的量angular器testing中使用lodash _.find?"

你几乎明白了:

 var mathsTopic = _.find(topics, { name: 'Maths' }); 

这可以理解为:find我的第一个主题的名称属性等于“math”的话题。

你有没有尝试使用filter

 var topic = element.all(by.repeater('topic in topics')) .filter(function (row) { return row.element(by.binding('topic.name')).getText().then(function(name) { return name === 'Maths'; }); }) .first() .map(function(row) { return { name: row.element(by.binding('topic.name')).getText()), description: row.element(by.binding('topic.description')).getText()) }; }); expect(topic).toEqual({ name: 'Maths', description: '2 + 2 = 4' });