【发布时间】:2019-12-22 00:12:58
【问题描述】:
我想从我的服务中测试getFund() 方法。我使用默认使用 jest 的 NestJS。
我不知道如何用玩笑来测试这条线:return await this.fundModel.findById(id);。有什么想法吗?
import { Injectable } from '@nestjs/common';
import { Model } from 'mongoose';
import { Fund } from '../../funds/interfaces/fund.interface';
import { InjectModel } from '@nestjs/mongoose';
@Injectable()
export class FundService {
constructor(
@InjectModel('Fund')
private readonly fundModel: Model<Fund>,
) {}
/*****
SOME MORE CODE
****/
async getFund(id: string): Promise<Fund> {
return await this.fundModel.findById(id);
}
}
编辑
感谢 slideshowp2 的回答,我写了这个测试。
describe('#getFund', () => {
it('should return a Promise of Fund', async () => {
let spy = jest.spyOn(service, 'getFund').mockImplementation(async () => {
return await Promise.resolve(FundMock as Fund);
});
service.getFund('');
expect(service.getFund).toHaveBeenCalled();
expect(await service.getFund('')).toEqual(FundMock);
spy.mockRestore();
});
});
问题是我在覆盖率报告中得到了这个结果:
当我悬停该行时,我得到statement not covered。
【问题讨论】:
标签: node.js unit-testing mongoose jestjs nestjs