admin管理员组

文章数量:1316329

I need to mock some functions in my controller, and reading the docks, looks like I should use the useMocker function to do that, the problem is that useMocker is never called.

This is my current setup:

reEach(async () => {
  const moduleRef = await Test.createTestingModule({
    imports: [NestjsFormDataModule, ConfigModule],
    controllers: [SubscriptionController],
  })
    .useMocker((token) => {
      console.log('TOKEN = ', token);
      if (token === SubscriptionController) {
        const mock = new SubscriptionControllerMock();
        mock.createCustomerPortalSession.mockImplementation(() => {});
        mock.createConfiguration.mockImplementation(() => {});
        mock.getConfigurations.mockImplementation(() => {});
        mock.subscribe.mockImplementation(() => {});
        mock.getConfiguration.mockImplementation(() => {});
        return mock;
      }
    })
    pile();

  app = moduleRef.createNestApplication();

  ({ agent } = await setupTestAuth(app, { admin: true }));
});

And the SubscriptionControllerMock class is just a class that extends the SubscriptionController but overrides the functions that I want to mock with jest.fn(), like this:

export class SubscriptionControllerMock extends SubscriptionController {
  createConfiguration = jest.fn();

  getConfiguration = jest.fn();

  getConfigurations = jest.fn();

  updateConfiguration = jest.fn();

  subscribe = jest.fn();

  deleteConfiguration = jest.fn();

  createCheckoutSession = jest.fn();

  createCustomerPortalSession = jest.fn();

  webhooks = jest.fn();
}

For some reason the useMocker is never called and I even tried using overrideProvider with the controller but also doesn't work and I can't find anywhere an example of someone that have done this. Please, help!

本文标签: testingHow to mock some controller functionsStack Overflow