title: NestJS: How can I mock ExecutionContext in canActivate
body: <p>I am having trouble in mocking ExecutionContext in Guard middleware.</p>
  <p>Here's my RoleGuard extends JwtGuard</p>
  <pre><code>@Injectable()
  export class RoleGuard extends JwtAuthGuard {
   ...
   async canActivate(context: ExecutionContext): Promise&lt;boolean&gt; {
      const request = context.switchToHttp().getRequest();
      const params = request.params;
  
      ...
   }
  }
  </code></pre>
  <p>This is what I am trying on my unit test.</p>
  <pre><code>let context: ExecutionContext = jest.genMockFromModule('@nestjs/common');
    
  context.switchToHttp = jest.fn().mockResolvedValue({
    getRequest: () =&gt; ({
     originalUrl: '/',
     method: 'GET',
     params: undefined,
     query: undefined,
     body: undefined,
    }),
    getResponse: () =&gt; ({
      statusCode: 200,
    }),
  });
      
  jest.spyOn(context.switchToHttp(), 'getRequest').mockImplementation(() =&gt; {
   return Promise.resolve(null);
  });
  </code></pre>
  <p>And I am getting this kind of error.</p>
  <pre><code>Cannot spy the getRequest property because it is not a function; undefined given instead
  </code></pre>
  <p>I would like you to suggest any other way to mock context. Thank you.</p>