【发布时间】:2021-04-14 15:10:06
【问题描述】:
我创建了一个服务,它的模块如下所示:
launchdarkly.module.ts
@Module({
providers: [LaunchdarklyService],
exports: [LaunchdarklyService],
imports: [ConfigService],
})
export class LaunchdarklyModule {}
(此服务/模块是让应用程序使用 LaunchDarkly 功能标记)
如果您愿意,我很乐意展示服务实现,但为了缩短这个问题,我跳过了它。重要的一点是该服务导入了ConfigService(它用于获取 LaunchDarkly SDK 密钥)。
但是如何测试Launchdarkly 服务呢?它从ConfigService 读取一个密钥,所以我想编写测试,其中ConfigService 具有各种值,但经过数小时的尝试,我无法弄清楚如何在测试中配置ConfigService。
这是测试:
launchdarkly.service.spec.ts
describe('LaunchdarklyService', () => {
let service: LaunchdarklyService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [LaunchdarklyService],
imports: [ConfigModule],
}).compile();
service = module.get<LaunchdarklyService>(LaunchdarklyService);
});
it("should not create a client if there's no key", async () => {
// somehow I need ConfigService to have key FOO=undefined for this test
expect(service.client).toBeUndefined();
});
it("should create a client if an SDK key is specified", async () => {
// For this test ConfigService needs to specify FOO=123
expect(service.client).toBeDefined();
});
})
我愿意接受任何非骇人听闻的建议,我只想对我的应用程序进行功能标记!
【问题讨论】:
标签: typescript dependency-injection nestjs launchdarkly