testing - Nestjs请求,测试中的拦截器
问题描述
我有 RateUpInterceptor 和这个添加计数率 1
@Injectable()
export class RateUpInterceptor implements NestInterceptor {
constructor() {}
intercept(context: ExecutionContext, next: CallHandler<any>): Observable<any> | Promise<Observable<any>> {
const http = context.switchToHttp();
const req = http.getRequest<IRequestAugmented>();
const res = http.getResponse<Response>();
return next.handle().pipe(
tap(async () => {
/** COUNT PLUS 1 **/
console.log('A');
}),
catchError((e) => {
return throwError(e);
}),
);
}
}
describe('RateUpInterceptor', () => {
let app: INestApplication;
let module: TestingModule;
beforeAll(async () => {
module = await Test.createTestingModule({
imports: [UserModule],
providers: [
{
provide: APP_INTERCEPTOR,
useFactory: () => {
return new RateUpInterceptor();
},
},
],
}).compile();
app = module.createNestApplication();
await app.init();
});
it('test', async () => {
request(app.getHttpServer())
.get(`valid url`)
.auth(token, { type: 'bearer' })
.expect(200).end(async () => {
console.log('B');
});
});
});
我使用请求通过 e2e 测试测试了这个拦截器。
我以为在全局拦截器“console.log('B')”执行后
但结果不同......结果等等
B
A
我该如何解决这个订单?
解决方案
推荐阅读
- mysql - MySQL 5.7.5+ 获得组的第一行
- ios - AutoLayout 约束动画从错误的点缩放
- android - 在不同的设备上设置 X 和 Y 坐标
- c# - 当我设置不同组合框的数据集时,`SelectedIndexChanged` 触发事件
- python - 在一行上声明多个变量的pythonic(3)方式
- mule - Scatter Gather 异常处理
- ruby-on-rails - Rails Factory Bot 访问工厂定义中传递的值
- java - 运行时未调用权限
- r - 在R中的矩阵中处理NA之前和之后的值
- javascript - 使用 FlexSlider 和 $(window).resize() 无限循环浏览器调整大小