Mocha 使用异步初始化代码进行测试
Mocha tests with async initialization code
我正在为 REST 客户端库编写测试,它必须 "login" 针对使用 OAuth 交换的服务。为了防止登录我要测试的每个端点,我想写一些 "test setup" 但我不确定我应该怎么做。
我的测试项目结构:
- 测试
- 端点-category1.spec.ts
- 端点-category2.spec.ts
如果我只有一个 "endpoint category" 我有这样的东西:
describe('Endpoint category 1', () => {
let api: Client = null;
before(() => {
api = new Client(credentials);
});
it('should successfully login using the test credentials', async () => {
await api.login();
});
it('should return xyz\'s profile', async () => {
const r: Lookup = await api.lookup('xyz');
expect(r).to.be.an('object');
});
});
我的问题:
由于 login() 方法是那里的第一个测试,它会起作用,并且客户端实例也可用于所有以下测试。但是,如何进行某种设置,使 "logged in api instance" 可用于我的其他测试文件?
公共代码应移至beforeEach
:
beforeEach(async () => {
await api.login();
});
此时 should successfully login using the test credentials
没有多大意义,因为它没有断言任何东西。
你看过 https://mochajs.org/#asynchronous-code 了吗?
你可以在你的测试函数中放入一个完成参数,你将得到一个你必须调用的回调。
完成()或完成(error/exception)
完成后也可以在之前和之后使用。
当调用 done() 时,mocha 知道你的异步代码已经完成。
啊。如果你想测试登录,你不应该提供这个连接给其他测试,因为在默认配置中不能保证测试顺序。
只是测试登录和注销之后。
如果您需要使用 "login-session" 进行更多测试,请使用 befores 描述一个新的测试。
describe('Endpoint category 1', () => {
let api: Client = null;
beforeEach(() => {
api = new Client(credentials);
});
afterEach(() => {
// You should make every single test to be ran in a clean environment.
// So do some jobs here, to clean all data created by previous tests.
});
it('should successfully login using the test credentials', async () => {
const ret = await api.login();
// Do some assert for `ret`.
});
context('the other tests', () => {
beforeEach(() => api.login());
it('should return xyz\'s profile', async () => {
const r: Lookup = await api.lookup('xyz');
expect(r).to.be.an('object');
});
});
});
我正在为 REST 客户端库编写测试,它必须 "login" 针对使用 OAuth 交换的服务。为了防止登录我要测试的每个端点,我想写一些 "test setup" 但我不确定我应该怎么做。
我的测试项目结构:
- 测试
- 端点-category1.spec.ts
- 端点-category2.spec.ts
如果我只有一个 "endpoint category" 我有这样的东西:
describe('Endpoint category 1', () => {
let api: Client = null;
before(() => {
api = new Client(credentials);
});
it('should successfully login using the test credentials', async () => {
await api.login();
});
it('should return xyz\'s profile', async () => {
const r: Lookup = await api.lookup('xyz');
expect(r).to.be.an('object');
});
});
我的问题:
由于 login() 方法是那里的第一个测试,它会起作用,并且客户端实例也可用于所有以下测试。但是,如何进行某种设置,使 "logged in api instance" 可用于我的其他测试文件?
公共代码应移至beforeEach
:
beforeEach(async () => {
await api.login();
});
此时 should successfully login using the test credentials
没有多大意义,因为它没有断言任何东西。
你看过 https://mochajs.org/#asynchronous-code 了吗?
你可以在你的测试函数中放入一个完成参数,你将得到一个你必须调用的回调。
完成()或完成(error/exception)
完成后也可以在之前和之后使用。
当调用 done() 时,mocha 知道你的异步代码已经完成。
啊。如果你想测试登录,你不应该提供这个连接给其他测试,因为在默认配置中不能保证测试顺序。
只是测试登录和注销之后。
如果您需要使用 "login-session" 进行更多测试,请使用 befores 描述一个新的测试。
describe('Endpoint category 1', () => {
let api: Client = null;
beforeEach(() => {
api = new Client(credentials);
});
afterEach(() => {
// You should make every single test to be ran in a clean environment.
// So do some jobs here, to clean all data created by previous tests.
});
it('should successfully login using the test credentials', async () => {
const ret = await api.login();
// Do some assert for `ret`.
});
context('the other tests', () => {
beforeEach(() => api.login());
it('should return xyz\'s profile', async () => {
const r: Lookup = await api.lookup('xyz');
expect(r).to.be.an('object');
});
});
});