将 TypeORM 存储库注入 NestJS 服务进行模拟数据测试 [英] Inject TypeORM repository into NestJS service for mock data testing

查看:40
本文介绍了将 TypeORM 存储库注入 NestJS 服务进行模拟数据测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这个问题中有一个关于如何做到这一点的冗长讨论.

There's a longish discussion about how to do this in this issue.

我已经尝试了许多建议的解决方案,但运气不佳.

I've experimented with a number of the proposed solutions but I'm not having much luck.

谁能提供一个具体的例子来说明如何使用注入的存储库和模拟数据来测试服务?

Could anyone provide a concrete example of how to test a service with an injected repository and mock data?

推荐答案

假设我们有一个非常简单的服务,它通过 id 查找用户实体:

Let's assume we have a very simple service that finds a user entity by id:

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

  async findUser(userId: string): Promise<UserEntity> {
    return this.userRepository.findOne(userId);
  }
}

然后您可以使用以下模拟工厂模拟 UserRepository(根据需要添加更多方法):

Then you can mock the UserRepository with the following mock factory (add more methods as needed):

// @ts-ignore
export const repositoryMockFactory: () => MockType<Repository<any>> = jest.fn(() => ({
  findOne: jest.fn(entity => entity),
  // ...
}));

使用工厂可确保每次测试都使用新的模拟.

Using a factory ensures that a new mock is used for every test.

describe('UserService', () => {
  let service: UserService;
  let repositoryMock: MockType<Repository<UserEntity>>;

  beforeEach(async () => {
    const module: TestingModule = await Test.createTestingModule({
      providers: [
        UserService,
        // Provide your mock instead of the actual repository
        { provide: getRepositoryToken(UserEntity), useFactory: repositoryMockFactory },
      ],
    }).compile();
    service = module.get<UserService>(UserService);
    repositoryMock = module.get(getRepositoryToken(UserEntity));
  });

  it('should find a user', async () => {
    const user = {name: 'Alni', id: '123'};
    // Now you can control the return value of your mock's methods
    repositoryMock.findOne.mockReturnValue(user);
    expect(service.findUser(user.id)).toEqual(user);
    // And make assertions on how often and with what params your mock's methods are called
    expect(repositoryMock.findOne).toHaveBeenCalledWith(user.id);
  });
});

为了类型安全和舒适,您可以为您的(部分)模拟使用以下类型(远非完美,当 jest 在即将发布的主要版本中开始使用 typescript 时,可能会有更好的解决方案):

For type safety and comfort you can use the following typing for your (partial) mocks (far from perfect, there might be a better solution when jest itself starts using typescript in the upcoming major releases):

export type MockType<T> = {
  [P in keyof T]?: jest.Mock<{}>;
};

这篇关于将 TypeORM 存储库注入 NestJS 服务进行模拟数据测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆