SpyOn TypeORM 存储库可更改单元测试 NestJS 的返回值

2024-03-05

我想对我的 TypeORM 数据库调用进行单元测试。我已经用有效数据模拟了我所有的 TypeORM 存储库。但我想监视存储库并更改 TypeORM 的返回值形式。我怎么做?

import {INestApplication} from '@nestjs/common';
import {Test} from '@nestjs/testing';
import {CommonModule} from '@src/common/common.module';
import {AuthService} from './auth.service';
import {Repository} from 'typeorm';
import {V3User} from '@src/database/entity/user.v3entity';
    
describe('AuthService', () => {
    let service: AuthService;
    let app: INestApplication;
        
    beforeEach(async () => {
        const module = await Test.createTestingModule({
            imports: [CommonModule.forRoot(`${process.env.DEV_ENV}`)],
            providers: [
                AuthService,     
                {provide: 'V3USER_REPOSITORY', useValue: mockRepositoryV3User()},
            ],
        }).compile();
    
        app = module.createNestApplication();
        await app.init();
    
        service = module.get<AuthService>(AuthService);
    });
    

    
    it('test auth service - with non existing user in v3 db', async () => {
                
        jest.spyOn(?????? , 'findOne').mockImplementation(() => undefined);
    
        const res = await service.loginUser("bad token");
    
        await expect(service.tokenBasedAuth('example bad token'))
            .rejects.toThrow('bad token exception');
    });
});

对于正常的测试用例,我像这样模拟数据库:

export const mockRepositoryV3User = () => ({
    metadata: {
        columns: [],
        relations: [],
    },
    findOne: async () =>
        Promise.resolve({
            id: 3,
            email: '[email protected] /cdn-cgi/l/email-protection',
            first_name: 'david',
            last_name: 'david',
            last_login: '2019-07-15',
            date_joined: '2019-07-15',
        }),
});

好吧,在最终开始测试和尝试想法之后,我发现这是一个有效的策略

  1. 假设我们已经设置了一个PhotoEntity具有基本属性,没有什么特别的(id、名称、描述等)
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';

@Entity()
export class Photo {
  @PrimaryGeneratedColumn()
  id: number;

  @Column({ length: 500 })
  name: string;

  @Column('text')
  description: string;

  @Column()
  filename: string;

  @Column('int')
  views: number;

  @Column()
  isPublished: boolean;
}
  1. 设置一个PhotoService如下所示(非常基本,但它会说明这一点):
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Photo } from './photo.entity';

@Injectable()
export class PhotoService {
  constructor(
    @InjectRepository(Photo)
    private readonly photoRepository: Repository<Photo>,
  ) {}

  async findAll(): Promise<Photo[]> {
    return await this.photoRepository.find();
  }
}
  1. We can useClass: Repository这样我们就不必做任何繁重的工作来设置用于测试的存储库类(存储库是从 TypeORM 包导入的。然后我们可以从模块中获取存储库并将其保存为一个值,以便于轻松实现)模拟并设置我们的测试,如下所示:
import { Test, TestingModule } from '@nestjs/testing';
import { PhotoService } from './photo.service';
import { getRepositoryToken } from '@nestjs/typeorm';
import { Photo } from './photo.entity';
import { Repository } from 'typeorm';

describe('PhotoService', () => {
  let service: PhotoService;
  // declaring the repo variable for easy access later
  let repo: Repository<Photo>;

  beforeEach(async () => {
    const module: TestingModule = await Test.createTestingModule({
      providers: [
        PhotoService,
        {
          // how you provide the injection token in a test instance
          provide: getRepositoryToken(Photo),
          // as a class value, Repository needs no generics
          useClass: Repository,
        },
      ],
    }).compile();

    service = module.get<PhotoService>(PhotoService);
    // Save the instance of the repository and set the correct generics
    repo = module.get<Repository<Photo>>(getRepositoryToken(Photo));
  });

  it('should be defined', () => {
    expect(service).toBeDefined();
  });

  it('should return for findAll', async () => {
    // mock file for reuse
    const testPhoto: Photo =  {
      id: 'a47ecdc2-77d6-462f-9045-c440c5e4616f',
      name: 'hello',
      description: 'the description',
      isPublished: true,
      filename: 'testFile.png',
      views: 5,
    };
    // notice we are pulling the repo variable and using jest.spyOn with no issues
    jest.spyOn(repo, 'find').mockResolvedValueOnce([testPhoto]);
    expect(await service.findAll()).toEqual([testPhoto]);
  });
});
  1. 针对指定文件或所有测试运行测试
▶ npm run test -- photo.service

> [email protected] /cdn-cgi/l/email-protection test ~/Documents/code/nestjs-playground
> jest "photo.service"

 PASS  src/photo/photo.service.spec.ts
  PhotoService
    ✓ should be defined (17ms)
    ✓ should return for findAll (4ms)  < -- test passes with no problem

Test Suites: 1 passed, 1 total
Tests:       2 passed, 2 total
Snapshots:   0 total
Time:        3.372s, estimated 4s
Ran all test suites matching /photo.service/i.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

SpyOn TypeORM 存储库可更改单元测试 NestJS 的返回值 的相关文章

随机推荐

  • 如何在 C# 中修改装箱值而不创建新对象?

    如何在 C 中修改装箱值而不创建新对象 例如 如果我有object o 5 我想改变盒装的值5 to 6 我怎样才能做到这一点 The o 6 将在堆上创建一个新对象并将对该对象的引用分配给o 还有其他方法可以更改盒装值吗 你可以自己 拳击
  • 从 ansible 运行 powershell 脚本并注册输出

    我正在尝试运行一个 Powershell 脚本 该脚本在远程 Windows PC 上使用 ansible 从 MSI 文件读取 ProductGUID powershell 在本地工作台上作为 ps1 文件运行 但从 ansible 中它
  • 请求超时并显示代码

    Error Domain NSURLErrorDomain Code 1001 The request timed out UserInfo NSUnderlyingError 0x608000244a70 Error Domain kCF
  • Spring Boot JSF 集成

    环境 雄猫8 春季启动 1 5 JSF 2 2 阿帕奇 MyFaces 春季MVC Code 我正在 Servlet 3 0 环境中集成 Spring Boot 和 JSF 2 2 配置类 JSFConfig java JSF 的配置 Co
  • 无法通用导入Python包

    假设我有以下目录结构 workspace init py ys manage init py manage py ys utils init py project dicts py 现在 假设我需要访问project dicts py in
  • 如何合并多个数组而不减慢编译器速度?

    添加这行代码会使我的编译时间从 10 秒缩短到 3 分钟 var resultsArray hashTagParticipantCodes prefixParticipantCodes asterixParticipantCodes att
  • C# 中引用类型和值类型有什么区别?

    几个月前有人问我这个问题 我无法详细解释 C 中引用类型和值类型有什么区别 我知道值类型是int bool float等 参考类型是delegate interface等等 或者这也是错误的吗 你能用专业的方式给我解释一下吗 你的例子有点奇
  • Windows 8 ARM(A.K.A.)吗? “Windows RT”有可供第三方开发人员使用的 Winapi (win32) 吗?

    Windows 8 for ARM 也称为 Windows RT 它是否具有与 Win32 API 等效的功能 I don t意味着它是否可以运行 Win32 x86 代码 但如果它具有 Win32 API可用的给第三方开发商 是的 ARM
  • Rake 任务只调用一次就执行两次

    我编写了一个非常简单的 rake 任务来尝试找到这个问题的根源 namespace foo do task bar environment do puts RUNNING end end 在控制台执行时rake foo bar输出是 RUN
  • SSIS 格式化货币输出

    在我的输出 CSV 文件中 有 4 列数据类型为货币 我的输出是平面文件 CSV 文件 我希望输出中的每一列都采用以下格式 这并没有发生 输出 CSV 文件中出现实际包含美分的列50 79 no 以以下结尾的列0正在压制0 40 8 零列看
  • Rest API 与客户端分离的多租户数据库

    我有一个带有组合键的多租户数据库 clientId docId 路由看起来像这样 api controller clientId docId 对于身份验证 我使用 全局 用户名 例如电子邮件 密码 通过 https 在每个请求的 http
  • Spring Data动态查询

    我正在尝试使用 spring 数据设置一个动态查询 基本上我有一个具有一堆特征的数组 我需要根据这些特征组装查询 几乎类似于 WHEREcharacteristic A ANDcharacteristic B AND特征 C 但特征的数量可
  • 单击 FusionTablesLayer 多边形时的事件

    每次客户端点击多边形时 我都需要在我的 JavaScript 中知道 我还需要知道它对应于我的融合表中的哪一行 有这样做的活动吗 有这样做的活动吗 FusionTables鼠标事件 https developers google com m
  • 将 functools 与 IronPython 结合使用

    我正在使用带有 IronPython 的 functools 库 它在开发机器上运行良好 但在生产中无法导入库 抛出异常 IronPython Runtime Exceptions ImportException 没有命名的模块 funct
  • 如何重命名 MongoDB 数据库?

    我的 MongoDB 数据库名称有一个拼写错误 我正在尝试重命名该数据库 I can copy http www mongodb org display DOCS Copy Database Commands然后像这样删除 db copyD
  • OpenGL 3.0 中 glDrawPixels 的替代品?

    所以我知道 glDrawPixels 已被弃用 有没有做同样事情的函数 我想过使用纹理 但它们是由当前矩阵修改的 与 glDrawPixels 绘制的像素不同 我想过使用纹理 但它们被当前矩阵修改了 当前矩阵 在 3 0 中已弃用 并在 3
  • 在 HTML 5 画布上创建 Reuleaux 多边形的函数

    我正在开展一个使用 HTML 5 画布设计系统艺术作品的项目 为了给我的作品带来更有机和多样化的感觉 我想要一个创建 reuleaux 多边形的函数 我认为可能有一种方法可以改变我的draw sharp polygon center pos
  • UITableViewCell 中的 UILabel 大小不正确(分配文本后的动画)

    文本在一个UILabel显示后闪烁 首先以省略号出现在单行上 然后占据其适合的 2 行 请注意 单元格高度没有改变 问题是这样的 标签 朋友们 快来参与并填满盒子吧 首先出现被截断的并在视图转换期间错位为 这种情况仅发生在装有 iOS 8
  • 为什么我应该更喜欢使用成员初始值设定项列表?

    我偏向于在构造函数中使用成员初始值设定项列表 但我早已忘记了其背后的原因 您是否在构造函数中使用成员初始值设定项列表 如果是这样 为什么 如果没有 为什么不呢 For POD https stackoverflow com a 146454
  • SpyOn TypeORM 存储库可更改单元测试 NestJS 的返回值

    我想对我的 TypeORM 数据库调用进行单元测试 我已经用有效数据模拟了我所有的 TypeORM 存储库 但我想监视存储库并更改 TypeORM 的返回值形式 我怎么做 import INestApplication from nestj