【问题标题】:Retrieving a Provider from TestingModule when using an interface使用接口时从 TestingModule 中检索提供者
【发布时间】:2020-06-03 09:42:18
【问题描述】:

我正在尝试从 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 实例能够直接调用方法?

【问题讨论】:

    标签: unit-testing testing nestjs


    【解决方案1】:

    您需要告诉get 方法执行代码中提供的相同注入令牌。在这种情况下

    const fooService = moduleFixture.get<FooService>('IFooService');
    

    【讨论】:

      猜你喜欢
      • 2022-01-07
      • 2012-07-07
      • 1970-01-01
      • 2023-03-28
      • 1970-01-01
      • 2019-08-31
      • 1970-01-01
      • 2022-11-04
      • 1970-01-01
      相关资源
      最近更新 更多