jestjs - 使用 Jest 和 nestjs-typegoose 测试 InjectModel
问题描述
我的控制器有一个参数是 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 (?) 的依赖项。请确保索引 [0] 处的参数 PollModel 在 PollsModule 上下文中可用。
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 */ ]
})
解决方案
poll 在全局模型中为“DbModel”,而 DbModel 在 appModel 中。所以testModel需要导入DbModel,然后修复!
推荐阅读
- javascript - 如何让 javascript 等待我的变量获取数据,然后显示数据。我正在使用 socket.io
- sql - SQL中基于多表两列的计数函数和分组函数
- azure - 根据 Azure 服务总线中的验证程序,远程证书无效
- ruby-on-rails - 如何最好地在基于 Rack 的应用程序中定义 ENV 变量?
- python - 我不知道如何将用户输入作为字符串处理
- javascript - 使用 JavaScript AJAX 调用多个 URL
- unit-testing - 当结果是函数时,无法测试为真(编译时出现语法错误。没有这样的 var:...)
- python - 缩放精灵大小
- regex - 如何在 Grafana 中使用正则表达式从指标中提取标签值?
- c++ - C++ 为我的新对象包装子对象数组的正确方法?