我找不到关于如何在 NestJS 中测试拦截器的任何解释
这个简单的示例拦截了一个 POST 查询,以将一个属性添加到正文中提供的示例模型中。
@Injectable()
export class SubscriberInterceptor implements NestInterceptor {
async intercept(
context: ExecutionContext,
next: CallHandler,
): Promise<Observable<ExampleModel>> {
let body: ExampleModel = context.switchToHttp().getRequest().body;
body = {
...body,
addedAttribute: 'example',
};
context.switchToHttp().getRequest().body = body;
return next.handle();
}
}
Run Code Online (Sandbox Code Playgroud)
我想测试拦截函数中发生了什么。
迄今为止:
const interceptor = new SubscriberInterceptor();
describe('SubscriberInterceptor', () => {
it('should be defined', () => {
expect(interceptor).toBeDefined();
});
describe('#intercept', () => {
it('should add the addedAttribute to the body', async () => {
expect(await interceptor.intercept(arg1, arg2)).toBe({ ...bodyMock, addedAttribute: …Run Code Online (Sandbox Code Playgroud)