【问题标题】:How to create a unit test with a mocked class that uses Testcafé's TestController?如何使用使用 Testcafé 的 TestController 的模拟类创建单元测试?
【发布时间】:2020-05-14 13:56:24
【问题描述】:

我有一个使用 typemoq 模拟类的测试,但我对它的一个功能有疑问。 在这个函数中,我调用 Testcafé 的 TestController 来使用特定的角色,但它没有按预期工作。

如何才能拥有一个可以在 testcafé 测试之外使用的 TestController 实例?

这是一个示例测试:

import {IMock} from "typemoq/Api/IMock";
import {Mock} from "typemoq";
import {Role, t} from "testcafe";
import { expect } from "chai";

class TestClass {
  public static async useRole(role: Role): Promise<boolean> {
    await t.useRole(role);
    return true;
  }
}

describe("test mock using Testcafé's TestController", () => {
  it('should switch role', async () => {
    const role = Role.anonymous();
    const mockedClass: IMock<typeof TestClass> = Mock.ofType<typeof TestClass>();

    mockedClass.setup(x => x.useRole(role)).returns(async (role) => {
      return TestClass.useRole(role);
    });

    const result = await mockedClass.object.useRole(role);

    expect(result).to.eq(true);
  });
});

调试时你应该得到这个:

提前致谢:)

【问题讨论】:

    标签: testing automation automated-tests e2e-testing testcafe


    【解决方案1】:

    只有在打开浏览器并且正在运行 TestCafe 测试时才能使用 TestCafe 函数,因此它们在自定义代码中不起作用。

    useRole 方法(以及大多数其他方法)也仅在打开浏览器时有效,因为浏览器和服务器代码在此方法中交互。 useRole 旨在从客户端获取 cookie 和本地存储。因此,即使您能够模拟 useRole 方法,它也会失去其逻辑中最重要的部分。

    【讨论】:

    • 感谢您的回复 :) 我同意嘲笑 useRole 不是解决方案,遗憾的是目前似乎别无选择
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-18
    • 1970-01-01
    • 2019-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-31
    相关资源
    最近更新 更多