gpt4 book ai didi

javascript - Jest 期望模拟函数被调用

转载 作者:行者123 更新时间:2023-11-30 11:03:41 25 4
gpt4 key购买 nike

我正在测试以下服务:

import { HttpException, HttpStatus, Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { extend } from 'lodash';
import { Repository } from 'typeorm';

import { DriverDTO } from './driver.dto';
import { DriverEntity } from './driver.entity';

@Injectable()
export class DriverService {
private logger = new Logger('DriverService');
constructor(
@InjectRepository(DriverEntity)
private driverRepository: Repository<DriverEntity>,
) { }

async create(clientId: string, data: DriverDTO): Promise<Partial<DriverEntity>> {
let driver = await this.driverRepository.findOne({ where: { clientId, driverId: data.driverId } });
this.logger.log(driver);
if (driver) {
throw new HttpException('Driver already exists', HttpStatus.CONFLICT);
}
driver = this.driverRepository.create(extend({ clientId }, data));
await this.driverRepository.save(driver);
return { driverId: driver.driverId, createdAt: driver.createdAt };
}
}

service.spec.ts如下:

import { Test, TestingModule } from '@nestjs/testing';

import { DriverService } from './driver.service';
import { MockType } from '../mock/mock.type';
import { plainToClass } from 'class-transformer';
import { repositoryMockFactory } from '../mock/repositoryMock.factory';
import { getRepositoryToken } from '@nestjs/typeorm';
import { DriverEntity } from './driver.entity';
import { Repository } from 'typeorm';
import { Pagination } from '../pagination/pagination';
import { HttpException, HttpStatus } from '@nestjs/common';
import { DriverDTO } from './driver.dto';

describe('DriverService', () => {
let service: DriverService;
let driverRepositoryMock: MockType<Repository<DriverEntity>>;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
DriverService,
{ provide: getRepositoryToken(DriverEntity), useFactory: repositoryMockFactory },
],
}).compile();

service = module.get<DriverService>(DriverService);
driverRepositoryMock = module.get(getRepositoryToken(DriverEntity));
});

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

describe('DriverService.create()', () => {
const driverDto: DriverDTO = {
driverId: 'one',
};

const driver: DriverEntity = plainToClass(DriverEntity, {
id: 1,
clientId: 'one',
driverId: 'one',
createdAt: '2019-02-11T04:11:00.766Z',
updatedAt: '2019-02-11T04:11:00.766Z',
});

it('should create driver if it does not already exist', () => {
const response: Partial<DriverEntity> = plainToClass(DriverEntity, {
driverId: driver.driverId,
createdAt: driver.createdAt,
});

driverRepositoryMock.findOne.mockReturnValue(undefined);
driverRepositoryMock.create.mockReturnValue(driver);
expect(service.create('one', driverDto)).resolves.toEqual(response);
expect(driverRepositoryMock.findOne).toHaveBeenCalledTimes(1);
expect(driverRepositoryMock.findOne).toHaveBeenCalledWith({ where: { clientId: 'one', driverId: driverDto.driverId } });
expect(driverRepositoryMock.create).toHaveBeenCalledTimes(1);
expect(driverRepositoryMock.create).toHaveBeenCalledWith({
clientId: driver.clientId,
driverId: driver.driverId,
});
expect(driverRepositoryMock.save).toHaveBeenCalledWith(driver);
});
});
});

但我不断收到以下错误:

● DriverService › DriverService.create() › should create driver if it does not already exist

expect(jest.fn()).toHaveBeenCalledTimes(1)

Expected mock function to have been called one time, but it was called zero times.

237 | expect(driverRepositoryMock.findOne).toHaveBeenCalledTimes(1);
238 | expect(driverRepositoryMock.findOne).toHaveBeenCalledWith({ where: { clientId: 'one', driverId: driverDto.driverId } });
> 239 | expect(driverRepositoryMock.create).toHaveBeenCalledTimes(1);
| ^
240 | expect(driverRepositoryMock.create).toHaveBeenCalledWith({
241 | clientId: driver.clientId,
242 | driverId: driver.driverId,

at Object.it (driver/driver.service.spec.ts:239:43)

Test Suites: 1 failed, 1 total
Tests: 1 failed, 8 passed, 9 total
Snapshots: 0 total
Time: 1.762s, estimated 6s
Ran all test suites matching /src\/driver\/driver.service.spec.ts/i.

我这样模拟存储库:

import { Repository, Entity } from 'typeorm';
import { MockType } from './mock.type';

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

我似乎找不到原因。任何帮助将不胜感激。

最佳答案

让你的测试函数异步等待,直到你的服务方法完全执行,然后再检查你的模拟的期望:

it('should create driver if it does not already exist', async () => {
// ^^^^^
// ...
await expect(service.create('one', driverDto)).resolves.toEqual(response);
//^^^^^

关于javascript - Jest 期望模拟函数被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56579571/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com