【发布时间】: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