【发布时间】:2021-06-17 16:51:18
【问题描述】:
我正在学习 NestJS,我正在尝试测试一个将存储库与 TypeORM 结合在一起的服务。它编译得很好,没有错误,但是当我运行测试时它会抛出一个错误。
我知道这是一个测试依赖问题,但我无法弄清楚。我正在尝试这个测试:
describe('JokesService', () => {
let service: JokesService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [],
imports: [
TypeOrmModule.forFeature([JokeEntity])
],
}).compile();
service = module.get<JokesService>(JokesService);
});
it('should be defined', () => {
expect(service).toBeDefined();
});
});
模块:
@Module({
providers: [JokesService],
imports: [
TypeOrmModule.forFeature([JokeEntity])
],
controllers: [JokesController],
})
export class JokesModule {}
服务:
@Injectable()
export class JokesService {
constructor(
@InjectRepository(JokeEntity)
private readonly jokeRepository: MongoRepository<JokeEntity>
) {}
}
主模块:
@Module({
imports: [
TypeOrmModule.forRoot({
type: 'mongodb',
url: dbUri,
entities: [__dirname + '/**/*.entity{.ts,.js}'],
ssl: true,
useUnifiedTopology: true,
useNewUrlParser: true,
}),
JokesModule
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
还有错误:
Nest cant resolve dependencies of the JokeEntityRepository (?).
Please make sure that the argument Connection at index [0] is available in the TypeOrmModule context.
Potential solutions:
- If Connection is a provider, is it part of the current TypeOrmModule?
- If Connection is exported from a separate @Module, is that module imported within TypeOrmModule?
@Module({
imports: [ /* the Module containing Connection */ ]
})
有人能告诉我测试依赖项中缺少什么吗?谢谢。
【问题讨论】:
-
你在这里测试的是什么?
-
老实说,只是确保生成命令附带的测试有效。我认为这只是为了测试模块是否有效。
-
如果你不知道你在测试什么,你可能不应该测试它。