使用Jest和nestjs-typegoose测试InjectModel

时间:2020-02-03 03:46:56

标签: jestjs nestjs

我的控制器有一个参数是InjectModel,代码:

 constructor(
      @InjectModel(Poll) private readonly model: ReturnModelType<typeof Poll>,
  ){
 }

,投票代码为:

import { prop, modelOptions, getModelForClass } from '@typegoose/typegoose';
import { ApiProperty, ApiPropertyOptions } from '@nestjs/swagger';

@modelOptions({
schemaOptions: {
timestamps: true
}
})
export class Poll {
@ApiProperty({

})
@prop({required: true})
title: string

@prop({required: true})
description: string

@prop()
poll: number

@prop({select: false})
userId: string

@prop()
userName: string

}

开玩笑的代码:

import { Poll } from '@libs/db/models/poll.model';
const module: TestingModule = await Test.createTestingModule({
  imports: [PollsModule],
  controllers: [PollsController],
  providers: [
    {
      provide: getModelForClass(Poll),
      useValue: getModelForClass(Poll)
    },
  ]
}).compile();

,我得到这个错误: Nest无法解析PollsController(?)的依赖项。请确保在PollsModule上下文中可用索引为[0]的PollModel参数。

Potential solutions:
- If PollModel is a provider, is it part of the current PollsModule?
- If PollModel is exported from a separate @Module, is that module imported within PollsModule?
  @Module({
    imports: [ /* the Module containing PollModel */ ]
  })

1 个答案:

答案 0 :(得分:0)

轮询在全局模型中为“ DbModel”,而DbModel在appModel中。所以testModel需要导入DbModel,然后修复!