pan*_*era 6 mongoose node.js gridfs typescript nestjs
我有一个@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: [AttachmentsService, {
provide: getConnectionToken(''),
useValue: {},
}],
}).compile();
service = module.get<AttachmentsService>(AttachmentsService);
Run Code Online (Sandbox Code Playgroud)
我意识到我必须模拟连接对象才能被 GridFS 调用,但现在我无法实际构建测试模块。
当您不添加显式连接名称时,nest.js 将使用默认连接令牌 DatabaseConnection,它由 nestjs-mongoose 包定义为常量:
export const DEFAULT_DB_CONNECTION = 'DatabaseConnection';
Run Code Online (Sandbox Code Playgroud)
所以你可以使用getConnectionToken('Database'):
providers: [AttachmentsService, {
provide: getConnectionToken('Database'),
useValue: {},
}]
Run Code Online (Sandbox Code Playgroud)
拉取请求已合并。你现在可以getConnectionToken()。
我创建了一个拉取请求,它允许在getConnectionToken()没有任何参数的情况下返回默认连接令牌。
| 归档时间: |
|
| 查看次数: |
3132 次 |
| 最近记录: |