【问题标题】:Mocking HTTP interceptors in Nest JS在 Nest JS 中模拟 HTTP 拦截器
【发布时间】:2020-05-27 19:45:49
【问题描述】:

我有一个在 Nest js 中提供身份验证拦截器的核心模块。 这是核心模块。

@Module({
    imports: [
    providers: [{
        provide: APP_INTERCEPTOR,
        useClass: LoggerInterceptor
    }
  ]
})
export class ConfModule {
}

这个 conf 模块是由 app 模块导入的。

@Module({
    imports: [
        ConfModule
    ]
})
export class AppModule {
}

这是我的LoggerInterceptor 课程

@Injectable()
export class LoggerInterceptor implements NestInterceptor {
    constructor(private readonly reflector: Reflector, private readonly connection: Connection) {
    }

    async intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
        // tslint:disable-next-line:no-console
        console.log(context.switchToHttp().getRequest().body);
        await this.connection.createEntityManager().save(context.switchToHttp().getRequest().body);
        return next.handle();
    }
}

我目前正在编写 E2E 测试,并希望覆盖记录器拦截器。这是我的 MockLoggerInterceptor

export class MockLoggerInterceptor extends LoggerInterceptor {
    reflex: Reflector;
    conn: Connection;

    constructor(reflector: Reflector, connection: Connection) {
        super(reflector, connection);
        this.reflex = reflector;
        this.conn = connection;
    }

    // @ts-ignore
    async intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
        // tslint:disable-next-line:no-console
        console.log(context.switchToHttp().getRequest().body);
        await this.conn.createEntityManager().save(context.switchToHttp().getRequest().body);
        return next.handle();
    }

}

这是我的测试套件

describe('PermissionController', () => {
    let applicationContext: INestApplication;
    let connection: Connection;
    let testUtils: TestUtils;
    let appHeader: App;

    beforeAll(async () => {
        const moduleRef: TestingModule = await Test.createTestingModule({
            imports: [AppModule]
        }).overrideInterceptor(LoggerInterceptor).useClass(MockLoggerInterceptor).compile();

        applicationContext = moduleRef.createNestApplication();
        connection = getConnection();
        await applicationContext.init();
        testUtils = new TestUtils(connection);
        appHeader = await testUtils.getAuthorisedApp();

    });
    it('Test of permission can be created', async () => {
        await request(applicationContext.getHttpServer())
            .post('/permissions')
            .set({
                'X-APP-CODE': appHeader.code,
                'X-APP-TOKEN': appHeader.token,
                'Authorisation': appHeader.token
            })
            .send(
                {
                    permissionName: 'newPermission'

                }
            ).expect(201);
    });

    afterAll(async () => {
        await connection.close();
        await applicationContext.close();

    });
});

我的测试仍然使用 conf 模块记录器而不是测试记录器。显然还有其他用例,但这是我能给出的最好的。

【问题讨论】:

    标签: javascript node.js typescript nestjs nestjs-config


    【解决方案1】:

    你必须使用overrideInterceptor而不是overrideProvider,因为模块的providers数组中没有提供拦截器:

    .overrideInterceptor(LoggerInterceptor).useClass(MockLoggerInterceptor)
    

    【讨论】:

    • 感谢@Kim Kern 显然我做到了。但它仍然没有工作。我已经尽力了。在我提出这个问题之前,我想确定一下。
    • 您的MockLoggerInterceptorLoggerInterceptor 具有相同的行为,不是吗?你怎么知道用的是哪一个?模拟是否只对全局拦截器失败?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    • 2014-05-29
    • 1970-01-01
    • 2019-06-26
    相关资源
    最近更新 更多