【问题标题】:Assert an array reduces to true断言数组归约为真
【发布时间】:2016-06-13 22:47:16
【问题描述】:

在其中一项测试中,我们需要断言 3 个元素之一存在。目前我们正在使用protractor.promise.all()Array.reduce()

var title = element(by.id("title")),
    summary = element(by.id("summary")),
    description = element(by.id("description"));

protractor.promise.all([
    title.isPresent(),
    summary.isPresent(),
    description.isPresent()
]).then(function (arrExists) {
    expect(arrExists.reduce(function(a,b) { return a || b; })).toBe(true);
});

有没有更好的方法 来用 Jasmine 解决这个问题而不明确解决承诺?我们需要自定义匹配器还是可以使用内置匹配器来解决?

【问题讨论】:

  • 请注意,您可以使用arrExists.some(function (a) {return a;}) 代替reduce。使用 Jasmine,您可以使用 done argument in the it callback,并在完成测试后调用它。我不认为有更好的方法。无论如何,您都需要等待承诺解决。
  • @trincot 谢谢你的意见。我可能应该提到expect() is "patched" in Protractor to implicitly resolve promises..但由于我必须将多个承诺的结果相互结合,在这种情况下可能无济于事。
  • 您应该提供false 作为初始值,以便它也适用于空数组。或者直接使用.some(Boolean)

标签: javascript arrays testing jasmine protractor


【解决方案1】:

您可以简单地使用单个选择器获取所有元素并断言计数优于零:

var title_summary_description = element.all(by.css("#title, #summary, #description"));
expect(title_summary_description.count()).toBeGreaterThan(0);

【讨论】:

    【解决方案2】:

    检查一下:

    let EC = protractor.ExpectedConditions;
    
    let title = EC.visibilityOf($("#title")),
        summary = EC.visibilityOf($("#summary")),
        description = EC.visibilityOf($("#description"));
    
    expect(EC.or(title, summary, description)() ).toBeTruthy('Title or summary or description should be visible on the page')
    

    请注意,我正在执行 ExpectedCondition 返回的函数 - 所以我得到的是该函数的结果(将被解析为布尔值的 Promise)而不是函数。

    如果需要,可以使用 .presenceOf() 代替 .visibilityOf()

    http://www.protractortest.org/#/api?view=ExpectedConditions.prototype.or

    【讨论】:

    • 使用预期条件的好主意,从来没想过。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2014-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-22
    • 1970-01-01
    • 2019-04-29
    相关资源
    最近更新 更多