小编Abh*_*ain的帖子

如何使用玩笑模拟链式函数调用?

我正在测试以下服务:

@Injectable()
export class TripService {
  private readonly logger = new Logger('TripService');

  constructor(
    @InjectRepository(TripEntity)
    private tripRepository: Repository<TripEntity>
  ) {}

  public async showTrip(clientId: string, tripId: string): Promise<Partial<TripEntity>> {
    const trip = await this.tripRepository
      .createQueryBuilder('trips')
      .innerJoinAndSelect('trips.driver', 'driver', 'driver.clientId = :clientId', { clientId })
      .where({ id: tripId })
      .select([
        'trips.id',
        'trips.distance',
        'trips.sourceAddress',
        'trips.destinationAddress',
        'trips.startTime',
        'trips.endTime',
        'trips.createdAt'
      ])
      .getOne();

    if (!trip) {
      throw new HttpException('Trip not found', HttpStatus.NOT_FOUND);
    }

    return trip;
  }
}
Run Code Online (Sandbox Code Playgroud)

我的存储库模拟:

export const repositoryMockFactory: () => MockType<Repository<any>> = jest.fn(() => ({ …
Run Code Online (Sandbox Code Playgroud)

node.js typescript jestjs typeorm nestjs

5
推荐指数
2
解决办法
3755
查看次数

开玩笑,预计模拟函数已被调用

我正在测试以下服务:

\n\n
import { HttpException, HttpStatus, Injectable, Logger } from '@nestjs/common';\nimport { InjectRepository } from '@nestjs/typeorm';\nimport { extend } from 'lodash';\nimport { Repository } from 'typeorm';\n\nimport { DriverDTO } from './driver.dto';\nimport { DriverEntity } from './driver.entity';\n\n@Injectable()\nexport class DriverService {\n  private logger = new Logger('DriverService');\n  constructor(\n    @InjectRepository(DriverEntity)\n    private driverRepository: Repository<DriverEntity>,\n  ) { }\n\n  async create(clientId: string, data: DriverDTO): Promise<Partial<DriverEntity>> {\n    let driver = await this.driverRepository.findOne({ where: { clientId, driverId: data.driverId } });\n    this.logger.log(driver);\n    if (driver) {\n      throw new HttpException('Driver already exists', …
Run Code Online (Sandbox Code Playgroud)

javascript node.js typescript jestjs nestjs

1
推荐指数
1
解决办法
4680
查看次数

标签 统计

jestjs ×2

nestjs ×2

node.js ×2

typescript ×2

javascript ×1

typeorm ×1