【问题标题】:Which is the right usage in Protractor test? await expect(someFn()).toBe.. or expect(await someFn()).toBe量角器测试中的正确用法是什么?等待期望(someFn()).toBe.. 或期望(await someFn()).toBe
【发布时间】:2020-12-19 01:15:47
【问题描述】:

对于 e2e 测试,我使用 Protractor 和 Jasmine 并禁用控制流。我知道 Protractor 使用 jasminewd2 一个 Jasmine 适配器。并且它增强了期望,以便在执行断言之前自动解包承诺。所以下面两个语句的效果是一样的。

expect(await someFn()).toBe..
await expect(someFn()).toBe..

是否存在上述语句表现不同的情况?推荐的选项是什么?示例规范here 遵循先等待的第二种方法。这样做有什么好处吗?

[编辑 1] 我发现的一个区别是处理 Promise 拒绝的方式。 Try catch 在这两种情况下都有效,但第二个选项允许捕获 Promise 方式。

it('should throw error 1', async () => {
  try {
    expect(await someFn()).toBe(1);
  } catch (err) {
    expect(err).toEqual('Unable to calculate');
  }
});

it('should throw error 2', async () => {
  await expect(someFn()).toBe(1)
                        .catch((err) => expect(err).toEqual('Unable to calculate'));
});

【问题讨论】:

    标签: angular async-await jasmine protractor


    【解决方案1】:

    你对 jasminewd2 的解释是对的。

    我会选择选项 1,将 await 放在 expect() 函数中,因为有实际的 Promise 等待。

    我认为包装器没有任何优势,因为 Protractor 中的控制流已被 async/await 取代。

    【讨论】:

    • 我看到的一个区别是 Promise 拒绝的处理方式。
    猜你喜欢
    • 2014-01-23
    • 2015-03-06
    • 1970-01-01
    • 2014-04-20
    • 2018-02-18
    • 2019-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多