【发布时间】:2019-06-23 17:13:50
【问题描述】:
如果未找到结果,我正在尝试测试捕获错误,对实体使用 TypeORM find 函数,并带有 where 的附加选项
我试图模拟该函数,但我似乎无法模拟该函数的 where 位。
服务文件
try {
historicalTypes = await this.historicalMappingDataTypesRepo.find({
where: {
HistoricalTypes: 'Contracts',
},
});
}
catch (e) {
if (e instanceof Error) {
this.logger.error(`Historical mapping type contracts could not found: ${e.message}`, e.stack);
} else {
this.logger.error(`Historical mapping type contracts could not found: ${e}`);
}
return;
}
测试
const mockHistoricalTypes: HistoricalTypes[] = [
{
HistoricalTypeID: 1,
HistoricalType: 'Contracts',
},
];
const mockHistoricalMappingTypesRepo = {
find: ({where}) => Promise.resolve(mockHistoricalMappingTypes),
};
beforeAll(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
HistoricalMappingSchedulerService,
{
provide: HistoricalMappingSchedulerRepository,
useValue: mockHistoricalMappingSchedulerRepository,
},
{
provide: getRepositoryToken(HistoricalMappingTypes),
useValue: mockHistoricalMappingTypesRepo,
},
],
}).compile();
service = module.get<HistoricalMappingSchedulerService>(HistoricalMappingSchedulerService);
});
it('should throw an error when historical type id is not found', async () => {
await mockHistoricalMappingTypesRepo.find({
where: {
HistoricalMappingType: 'Testing',
},
});
const mockFn = () => service.ContractHistoricalMapping();
expect(mockFn).toThrowError();
});
【问题讨论】:
-
还有问题吗? :-)
标签: javascript node.js typescript nestjs typeorm