如何在我的量角器测试中使用lodash _.find?

时间:2015-03-09 14:27:01

标签: angularjs node.js protractor lodash

我想使用Lodash find函数来使我的量角器测试更加健壮。

而不是

    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');    
    });

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

2 个答案:

答案 0 :(得分:2)

你几乎得到了它:

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

可以理解为:在主题中找到第一个主题,其名称属性等于'数学'。

答案 1 :(得分:2)

您是否尝试使用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'
});