【发布时间】:2021-04-23 11:02:11
【问题描述】:
我有带有 canActivate 功能的 authGuard 服务```
canActivate(context: ExecutionContext): boolean |承诺 |可观察的``` 为了使用一些标头值和所有测试此函数,我需要传递 ExecutionContext。 那么如何创建虚拟 ExecutionContext 来测试这个功能呢? 这是ExecutionContext的接口。const ctx = GqlExecutionContext.create(context); const { req } = ctx.getContext();
export interface ExecutionContext extends ArgumentsHost {
/**
* Returns the *type* of the controller class which the current handler belongs to.
*/
getClass<T = any>(): Type<T>;
/**
* Returns a reference to the handler (method) that will be invoked next in the
* request pipeline.
*/
getHandler(): Function;
}
export interface ArgumentsHost {
/**
* Returns the array of arguments being passed to the handler.
*/
getArgs<T extends Array<any> = any[]>(): T;
/**
* Returns a particular argument by index.
* @param index index of argument to retrieve
*/
getArgByIndex<T = any>(index: number): T;
/**
* Switch context to RPC.
* @returns interface with methods to retrieve RPC arguments
*/
switchToRpc(): RpcArgumentsHost;
/**
* Switch context to HTTP.
* @returns interface with methods to retrieve HTTP arguments
*/
switchToHttp(): HttpArgumentsHost;
/**
* Switch context to WebSockets.
* @returns interface with methods to retrieve WebSockets arguments
*/
switchToWs(): WsArgumentsHost;
/**
* Returns the current execution context type (string)
*/
getType<TContext extends string = ContextType>(): TContext;
}```
【问题讨论】:
标签: typescript nestjs