Jest 测试用例在 nestjs 中失败
Jest test case is failing in nestjs
我在服务中的 nestjs 中有以下方法 class。
async findOne(id: string): Promise<Certificate> {
const result = await this.certificateRepository.findOne({ id });
if (!result) {
throw new NotFoundException(`Certificate with ID does not exist`);
}
return result;
}
我在下面写了相同的测试用例。
it('should find one certificate', async () => {
const id = '1';
const certificate = await service.findOne(id);
expect(repository.findOne).toHaveBeenCalledTimes(1);
expect(repository.findOne).toHaveBeenCalledWith(id);
expect(certificate).toBeInstanceOf(Certificate);
});
我遇到了同样的错误。
我不确定为什么返回 object
而不是 value
。如果我不能修改这个方法,我可以修改这个测试用例以重新启动 object
?
我应该在测试用例中做哪些更改?
将断言更新为以下内容:
expect(repository.findOne).toHaveBeenCalledWith({ id: "1" });
这将匹配代码使用 id
调用 certificateRepository.findOne
的方式。
我在服务中的 nestjs 中有以下方法 class。
async findOne(id: string): Promise<Certificate> {
const result = await this.certificateRepository.findOne({ id });
if (!result) {
throw new NotFoundException(`Certificate with ID does not exist`);
}
return result;
}
我在下面写了相同的测试用例。
it('should find one certificate', async () => {
const id = '1';
const certificate = await service.findOne(id);
expect(repository.findOne).toHaveBeenCalledTimes(1);
expect(repository.findOne).toHaveBeenCalledWith(id);
expect(certificate).toBeInstanceOf(Certificate);
});
我遇到了同样的错误。
我不确定为什么返回 object
而不是 value
。如果我不能修改这个方法,我可以修改这个测试用例以重新启动 object
?
我应该在测试用例中做哪些更改?
将断言更新为以下内容:
expect(repository.findOne).toHaveBeenCalledWith({ id: "1" });
这将匹配代码使用 id
调用 certificateRepository.findOne
的方式。