Mat*_*ler 4 unit-testing interceptor typescript jestjs nestjs
我找不到关于如何在 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: 'example' });
});
});
});
Run Code Online (Sandbox Code Playgroud)
我的问题:我应该只模拟arg1: ExecutionContext
andarg2: CallHandler
吗?如果是这样,如何模拟arg1
和arg2
?否则我应该如何进行?
你是对的,你应该模拟arg1
and arg2
,然后将它们传递给intercept
方法,这是解决方案:
SubscriberInterceptor.ts
:
interface ExecutionContext {
switchToHttp(): any;
}
interface CallHandler {
handle(): any;
}
interface Observable<T> {}
interface ExampleModel {}
interface NestInterceptor {
intercept(context: ExecutionContext, next: CallHandler): Promise<Observable<ExampleModel>>;
}
export class SubscriberInterceptor implements NestInterceptor {
public 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)
单元测试,模拟链式方法 executionContext
import { SubscriberInterceptor } from './';
const interceptor = new SubscriberInterceptor();
const executionContext = {
switchToHttp: jest.fn().mockReturnThis(),
getRequest: jest.fn().mockReturnThis()
};
const callHandler = {
handle: jest.fn()
};
describe('SubscriberInterceptor', () => {
it('should be defined', () => {
expect(interceptor).toBeDefined();
});
describe('#intercept', () => {
it('t1', async () => {
(executionContext.switchToHttp().getRequest as jest.Mock<any, any>).mockReturnValueOnce({
body: { data: 'mocked data' }
});
callHandler.handle.mockResolvedValueOnce('next handle');
const actualValue = await interceptor.intercept(executionContext, callHandler);
expect(actualValue).toBe('next handle');
expect(executionContext.switchToHttp().getRequest().body).toEqual({
data: 'mocked data',
addedAttribute: 'example'
});
expect(callHandler.handle).toBeCalledTimes(1);
});
});
});
Run Code Online (Sandbox Code Playgroud)
单元测试结果:
interface ExecutionContext {
switchToHttp(): any;
}
interface CallHandler {
handle(): any;
}
interface Observable<T> {}
interface ExampleModel {}
interface NestInterceptor {
intercept(context: ExecutionContext, next: CallHandler): Promise<Observable<ExampleModel>>;
}
export class SubscriberInterceptor implements NestInterceptor {
public 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)
归档时间: |
|
查看次数: |
4327 次 |
最近记录: |