NestJS TypeORM InjectRepository无法读取未定义的属性“prototype”

问题描述 投票:3回答:3

试图进行单元测试。得到以下错误:

TypeError:无法读取未定义的属性“prototype”

导出类UserService {

构造函数(@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

顺便说一下,使用Postman的API检查了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)
});
typescript jestjs nestjs typeorm
3个回答
1
投票

我看了你在Kim Kern(https://github.com/rankery/wof-server)的评论中提供的项目

您正在使用桶文件(src/user/index.ts),导出UserModule

export * from './user.module';

我猜你以后会使用这个桶文件来导入模块。

现在,每次导入桶文件的内容时,都会执行src/user/user.module.ts构建版本中包含的代码,其中包括UserModule类的修饰,这反过来会使Typeorm尝试构建一个Repository,这会导致错误。

您应该从src/user/index.ts中删除此导出(或者只是删除文件)并修复由此删除导致的损坏的导入,它应该可以工作。


0
投票

我刚刚将User实体传递给Repository并且它可以工作。

@Injectable()
export class UserService {
    constructor(
        @InjectRepository(User)
        private readonly userRepository: Repository<User>
    ) { }

}

结账文档来自:https://docs.nestjs.com/techniques/database。他们有很好的文档。


0
投票

当您导入TypeOrmModule.forFeature(...)时,您还必须导入TypeOrmModule.forRoot(...)。但是在单元测试中,您可能不希望使用数据库,而是模拟所有依赖项。

您的控制器不应该直接访问数据库,这就是服务的用途。因此,如果您想测试您的控制器并且它只注入服务,您应该只声​​明一个AuthService模拟而不导入任何东西:

const module = await Test.createTestingModule({
    controllers: [AuthController],
    providers: [{
        provide: AuthService,
        useValue: authServiceMock
    }]
}).compile()

如果你想测试你的AuthService并且只注入存储库,则声明你的mockRepository并将其他所有内容保留。

© www.soinside.com 2019 - 2024. All rights reserved.