小编pan*_*era的帖子

NestJS - Mongoose @InjectConnection 单元测试

我有一个@InjectConnection在其构造函数中使用装饰器的服务。

我无法为此服务实例化 testingModule。抛出以下错误:Nest can't resolve dependencies of the AttachmentsService (?, winston). Please make sure that the argument at index [0] is available in the TestModule context.

服务构造函数:

  constructor(@InjectConnection() private readonly mongooseConnection: Mongoose,
              @Inject(Modules.Logger) private readonly logger: Logger) {
    this.attachmentGridFsRepository = gridfs({
      collection: 'attachments',
      model: Schemas.Attachment,
      mongooseConnection: this.mongooseConnection,
    });

    this.attachmentRepository = this.attachmentGridFsRepository.model;
  }
Run Code Online (Sandbox Code Playgroud)

测试模块构造函数:

const module: TestingModule = await Test.createTestingModule({
  imports: [
    WinstonModule.forRoot({
      transports: [
        new transports.Console({
          level: 'info',
          handleExceptions: false,
          format: format.combine(format.json()),
        }),
      ],
    }),
  ],
  providers: …
Run Code Online (Sandbox Code Playgroud)

mongoose node.js gridfs typescript nestjs

6
推荐指数
1
解决办法
3132
查看次数

标签 统计

gridfs ×1

mongoose ×1

nestjs ×1

node.js ×1

typescript ×1