【问题标题】:NestJs TypeOrm Custom Repository Unit Test CaseNestJs TypeOrm 自定义存储库单元测试用例
【发布时间】:2021-06-07 21:09:26
【问题描述】:

我的 nestjs 项目使用 typeorm 自定义存储库。

@EntityRepository(MyEntity)
export class MyRepository extends AbstractRepository<MyEntity> {
    
    public operation1(entity) {

        /**
         * Few lines here   
        */
        this.manager.save(entity);
    }
 }

我正在尝试使用 jest 为方法 operation1 编写单元测试用例,但我无法模拟 this.manager.。 有没有办法模拟 this.manager. 方法?

【问题讨论】:

    标签: nestjs typeorm typeorm-datamapper


    【解决方案1】:

    如果您使用的是 Jest。让我们试试这个方法:

    let myRepository: Repository<MyRepository>;
    
    jest
      .spyOn(myRepository, 'manage')
      .mockImplementation(() => Promise.resolve(entity);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-07
      • 2019-11-27
      • 1970-01-01
      • 2020-07-02
      • 2021-01-07
      • 2013-02-20
      • 1970-01-01
      • 2022-07-07
      相关资源
      最近更新 更多