我试图运行NestJ测试,但得到了以下错误
Nest can't resolve dependencies of the (?). Please make sure that the argument at index [0] is available in the RootTestModule context.
本项目使用猫鼬连接到MongoDB。
您可以通过在这个repo https://github.com/kruyvanna/nestjs-test-error中运行代码来再现错误。
提前谢谢你
发布于 2022-04-14 11:31:02
您将得到该错误,因为您有以下模块:
const module: TestingModule = await Test.createTestingModule({
providers: [CatService],
}).compile();
而这个提供者:
@Injectable()
export class CatService {
constructor(@InjectModel(Cat.name) private catModel: Model<CatDocument>) {}
}
而且没有办法知道catModel
的值是什么,因为它的提供者令牌没有在测试模块中注册。
要解决这个问题,您可以注册它,就像医生的节目
const module: TestingModule = await Test.createTestingModule({
providers: [CatService, { provide: getModelToken(Cat.name), useValue: jest.fn() }],
}).compile();
https://stackoverflow.com/questions/71869915
复制相似问题