NestJS TypeORM InjectRepository 无法读取未定义的 属性 'prototype'
NestJS TypeORM InjectRepository Cannot read property 'prototype' of undefined
正在尝试进行单元测试。
出现以下错误:
TypeError: Cannot read property 'prototype' of undefined
export class UserService {
constructor(@InjectRepository(User) private readonly userRepository:
Repository < User>) { }
spec.ts:
describe('AuthController', () => {
let authController: AuthController;
let authService: AuthService;
let mockRepository = {
};
beforeEach(async () => {
const module = await Test.createTestingModule({
imports: [
TypeOrmModule.forFeature([User]),
],
controllers: [AuthController],
providers: [AuthService, {
provide: getRepositoryToken(User),
useValue: mockRepository
}]
}).compile()
authService = module.get<AuthService>(AuthService);
authController = module.get<AuthController>(AuthController)
});
有人可以分享解决方案吗?
更多信息:
看来 typeorm
有问题
beforeEach(async () => {
const module = await Test.createTestingModule({
}).compile()
authService = module.get<AuthService>(AuthService);
authController = module.get<AuthController>(AuthController)
});
使用这段代码,我得到了完全相同的错误。所以唯一的问题是将 typeorm
添加到此测试模块。
所以失败是因为依赖:AuthController->AuthService->UserService->TypeORM
顺便说一句,刚刚使用 API 和 Postman 检查了 UserService
,它工作正常。
仍然没有结果:
module = await Test.createTestingModule({
controllers: [AuthController],
components: [
{
provide: AuthService,
useValue: {}
},
{
provide: UserService,
useValue: {}
},
{
provide: getRepositoryToken(User),
useValue: {}
}
],
providers: [
{
provide: AuthService,
useValue: {}
},
{
provide: UserService,
useValue: {}
},
{
provide: getRepositoryToken(User),
useValue: {}
}
]
}).compile()
this.authController = module.get<AuthController>(AuthController)
还有
class AuthServiceMock {
logIn(userName) {
return { id:100, isDeleted:false, login:"login", password:"password"};
}
signUp() {
return { expireIn: 3600, token:"token" };
}
}
describe('AuthController', () => {
let module: TestingModule;
let authController: AuthController;
let authService: AuthService;
beforeEach(async () => {
module = await Test.createTestingModule({
controllers: [AuthController],
components: [
],
providers: [
{
provide: AuthService,
useClass: AuthServiceMock
},
]
}).compile()
this.authController = module.get<AuthController>(AuthController)
});
我刚刚将 User 实体传递给了 Repository,它起作用了。
@Injectable()
export class UserService {
constructor(
@InjectRepository(User)
private readonly userRepository: Repository<User>
) { }
}
从这里查看文档:https://docs.nestjs.com/techniques/database。他们有很好的文档。
当您导入 TypeOrmModule.forFeature(...)
时,您还必须导入 TypeOrmModule.forRoot(...)
。但是在单元测试中,您可能不想使用数据库,而是模拟所有依赖项。
您的控制器不应直接访问数据库,这就是该服务的用途。所以如果你想测试你的控制器并且它只注入服务,你应该只声明一个 AuthService
模拟而不导入任何东西:
const module = await Test.createTestingModule({
controllers: [AuthController],
providers: [{
provide: AuthService,
useValue: authServiceMock
}]
}).compile()
如果你想测试你的 AuthService
并且它只注入存储库声明你的 mockRepository 并留下其他一切。
我查看了您在 Kim Kern (https://github.com/rankery/wof-server) 下的评论中提供的项目
您正在使用桶文件 (src/user/index.ts
),导出 UserModule
export * from './user.module';
我猜你稍后会使用这个桶文件来导入模块。
现在,每次导入桶文件的内容时,都会执行 src/user/user.module.ts
构建版本中包含的代码,其中包括 UserModule
class],这又会使 Typeorm 尝试构建一个存储库,从而导致错误。
您应该从 src/user/index.ts
中删除此导出(或简单地删除文件)并修复此删除导致的损坏的导入,它应该可以工作。
我花了好几个小时弄明白了,它起作用了
async findAll() {
return await this.userRepository.createCursor(this.userRepository.find()).toArray();
}
正在尝试进行单元测试。 出现以下错误:
TypeError: Cannot read property 'prototype' of undefined
export class UserService {
constructor(@InjectRepository(User) private readonly userRepository: Repository < User>) { }
spec.ts:
describe('AuthController', () => {
let authController: AuthController;
let authService: AuthService;
let mockRepository = {
};
beforeEach(async () => {
const module = await Test.createTestingModule({
imports: [
TypeOrmModule.forFeature([User]),
],
controllers: [AuthController],
providers: [AuthService, {
provide: getRepositoryToken(User),
useValue: mockRepository
}]
}).compile()
authService = module.get<AuthService>(AuthService);
authController = module.get<AuthController>(AuthController)
});
有人可以分享解决方案吗?
更多信息:
看来 typeorm
beforeEach(async () => {
const module = await Test.createTestingModule({
}).compile()
authService = module.get<AuthService>(AuthService);
authController = module.get<AuthController>(AuthController)
});
使用这段代码,我得到了完全相同的错误。所以唯一的问题是将 typeorm
添加到此测试模块。
所以失败是因为依赖:AuthController->AuthService->UserService->TypeORM
顺便说一句,刚刚使用 API 和 Postman 检查了 UserService
,它工作正常。
仍然没有结果:
module = await Test.createTestingModule({
controllers: [AuthController],
components: [
{
provide: AuthService,
useValue: {}
},
{
provide: UserService,
useValue: {}
},
{
provide: getRepositoryToken(User),
useValue: {}
}
],
providers: [
{
provide: AuthService,
useValue: {}
},
{
provide: UserService,
useValue: {}
},
{
provide: getRepositoryToken(User),
useValue: {}
}
]
}).compile()
this.authController = module.get<AuthController>(AuthController)
还有
class AuthServiceMock {
logIn(userName) {
return { id:100, isDeleted:false, login:"login", password:"password"};
}
signUp() {
return { expireIn: 3600, token:"token" };
}
}
describe('AuthController', () => {
let module: TestingModule;
let authController: AuthController;
let authService: AuthService;
beforeEach(async () => {
module = await Test.createTestingModule({
controllers: [AuthController],
components: [
],
providers: [
{
provide: AuthService,
useClass: AuthServiceMock
},
]
}).compile()
this.authController = module.get<AuthController>(AuthController)
});
我刚刚将 User 实体传递给了 Repository,它起作用了。
@Injectable()
export class UserService {
constructor(
@InjectRepository(User)
private readonly userRepository: Repository<User>
) { }
}
从这里查看文档:https://docs.nestjs.com/techniques/database。他们有很好的文档。
当您导入 TypeOrmModule.forFeature(...)
时,您还必须导入 TypeOrmModule.forRoot(...)
。但是在单元测试中,您可能不想使用数据库,而是模拟所有依赖项。
您的控制器不应直接访问数据库,这就是该服务的用途。所以如果你想测试你的控制器并且它只注入服务,你应该只声明一个 AuthService
模拟而不导入任何东西:
const module = await Test.createTestingModule({
controllers: [AuthController],
providers: [{
provide: AuthService,
useValue: authServiceMock
}]
}).compile()
如果你想测试你的 AuthService
并且它只注入存储库声明你的 mockRepository 并留下其他一切。
我查看了您在 Kim Kern (https://github.com/rankery/wof-server) 下的评论中提供的项目
您正在使用桶文件 (src/user/index.ts
),导出 UserModule
export * from './user.module';
我猜你稍后会使用这个桶文件来导入模块。
现在,每次导入桶文件的内容时,都会执行 src/user/user.module.ts
构建版本中包含的代码,其中包括 UserModule
class],这又会使 Typeorm 尝试构建一个存储库,从而导致错误。
您应该从 src/user/index.ts
中删除此导出(或简单地删除文件)并修复此删除导致的损坏的导入,它应该可以工作。
我花了好几个小时弄明白了,它起作用了
async findAll() {
return await this.userRepository.createCursor(this.userRepository.find()).toArray();
}