如果期望失败,如何通过测试

How to pass a test if expect fails

我有这个代码

it('This should pass anyway', function (done) {
  testObj.testIt(regStr);
});

测试对象

this.testIt = function (regStr) {
  selector.count().then(function (orgCount) {
    for (var curr = 0; curr < count; curr++) {
       checkField(curr, regStr);
    }
  });
};

function checkField(curr, regStr) {
  selector.get(curr).all(by.tagName('li')).get(0).getInnerHtml().then(function (text) {
    expect(text).to.match(regStr, curr + '#ERR');
  });
}

如果其中之一期望失败,则测试失败。我该如何处理?我的意思是 - 我能以某种方式计算通过和失败的 expect()ations 和 return 吗?或者,至少,不要让测试在第一个错误时中断。

我试过 try-catch,但没有什么好事发生。

it('This should pass anyway', function (done) {
  try {
    testObj.testIt(regStr);
  } catch (e) {
    console.log('#err' + e);
  }
});

然后我想使用 done(),但还没有找到任何类似的例子。你能帮帮我吗? 对不起我的英语

更新

您可以 return 空值或来自 checkField() 的字符串,将它们连接起来,并期望数组为空:

this.testIt = function (regStr) {
  selector.count().then(function (orgCount) {
    var errors = [];
    for (var curr = 0; curr < orgCount; curr++) {
       var e = checkField(curr, regStr);
       if (e) { errors.push(e); }
    }
    assert.equal(0, errors.length, errors);
  });
};

更简洁的方法是使用 map() 将数据收集到数组中:

var data = selector.map(function (elm) {
    return elm.element(by.tagName('li')).getText();
});

expect(data).toEqual(["test1", "test2", "test3"]);