Javascript 动态异步mocha测试
我有一个Javascript 动态异步mocha测试,javascript,unit-testing,testing,mocha.js,Javascript,Unit Testing,Testing,Mocha.js,我有一个服务类,扩展了50个其他“服务”。虽然每个服务都有自己的测试,但我想为所有服务共享功能编写一个测试套件(我有一个每个服务都必须实现的方法thing) 为了测试thing,每个服务都有一个函数thingConfig,该函数返回可以运行的配置数组thing。我想做以下工作: description('service',()=>{ 让我们一起来; 之前(async()=>configs=await service.thingConfig()) configs.forEach(配置=>{ 它(c
服务
类,扩展了50个其他“服务”。虽然每个服务都有自己的测试,但我想为所有服务共享功能编写一个测试套件(我有一个每个服务都必须实现的方法thing
)
为了测试thing
,每个服务都有一个函数thingConfig
,该函数返回可以运行的配置数组thing
。我想做以下工作:
description('service',()=>{
让我们一起来;
之前(async()=>configs=await service.thingConfig())
configs.forEach(配置=>{
它(config.name+‘应该运行东西’,()=>{
thing=wait service.thing(配置);
期待(事物)到。。。。
});
});
})
是否有可能基于异步数据进行此动态测试(forEach)?为了使其工作,您必须创建一些虚拟it案例 请看以下示例:
describe('Dummy spec', () => {
before(async () => {
const configs = await service.thingConfig();
describe('Generated spec', () => {
configs.forEach((config) => {
it(`Test for config: ${config}`, async () => {
const thing = await service.thing(config);
expect(thing).to....
});
});
});
});
it('Dummy test case, so before is executed', () => assert.strictEqual(1, 1));
});
假设一切顺利,您将看到以下形式的结果:
Dummy spec
√ Dummy test case, so before is executed
Generated spec
√ Test for config: test1
√ Test for config: test2
√ Test for config: test3
√ Test for config: test4
5 passing (3s)
你面临的问题是什么?可能缺少一个异步关键字
it(config.name+'should run thing',async()=>{
这真的是最好的方法吗?对我来说,这看起来非常难看和讨厌…为什么不为延迟的根套件使用--delay
?