我正在尝试从NestJS的测试模块中检索提供程序,但是使用接口从模块中提供该模块时无法解析该模块。
这是我到目前为止所拥有的:
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule],
}).compile()
...
const fooService = moduleFixture.get<FooService>(FooService) // Nest could not find FooService element
当导出FooService的模块正在执行类似操作时...
@Global()
@Module({
providers: [{ provide: 'IFooService', useClass: FooService }],
exports: [{ provide: 'IFooService', useClass: FooService }],
})
export class FooModule {}
如何获取FooService实例以直接调用方法?
答案 0 :(得分:0)
您需要告诉get
方法完成代码中提供的相同注入令牌。在这种情况下
const fooService = moduleFixture.get<FooService>('IFooService');