【发布时间】:2022-01-06 15:52:56
【问题描述】:
我有一个服务(在 Nestjs 中),我想用 Jest 测试函数异常。
我的服务:
class MyService{
public throwError(ms: string) {
throw new UnprocessableEntityException(ms);
}
}
我想测试它,但我不知道该怎么做。
【问题讨论】:
标签: unit-testing jestjs nestjs
我有一个服务(在 Nestjs 中),我想用 Jest 测试函数异常。
我的服务:
class MyService{
public throwError(ms: string) {
throw new UnprocessableEntityException(ms);
}
}
我想测试它,但我不知道该怎么做。
【问题讨论】:
标签: unit-testing jestjs nestjs
I feel like jest's docs point this out pretty well,但作为一个简单的例子,对于上面的代码,你只需要一些简单的东西,比如
describe('MyuService', () => {
describe('throwError', () => {
it('should throw an UnprocessableEntityException', () => {
const myService = new MyService();
expect(
() => myService.throwError('some string')
).toThrow(new UnprocessableEntityException('some string'));
})
})
})
【讨论】: