nestjs 上的模拟 rabbitMQ

问题描述 投票:0回答:0

我正在尝试在 nestjs 应用程序上编写端到端测试,并且我有一个 rabbitMQ 连接

    type MockType<T> = {
      [P in keyof T]?: jest.Mock<object>;
    };
    const mockFactory: () => MockType<AmqpConnection> = jest.fn(() => ({
      publish: jest.fn(() => AmqpConnection),
    }));

    const moduleRef = await Test.createTestingModule({
      providers: [
        RabbitMQModule,
        { provide: AmqpConnection, useFactory: mockFactory },
      ],
      imports: [ChallengeModule],
    })
      .useMocker((token) => {
        if (token === ChallengeService) {
          return challengeService;
        } else {
          const mockMetadata = moduleMocker.getMetadata(
            token,
          ) as MockFunctionMetadata<any, any>;
          const Mock = moduleMocker.generateFromMetadata(mockMetadata);
          return new Mock();
        }
      })
      .overrideProvider(ChallengeService)
      .useValue(challengeService)
      .compile();
    app = moduleRef.createNestApplication();
    await app.init();
  });

我得到错误:错误 [AmqpConnection] 与 RabbitMQ 代理断开连接(默认) 类型错误:无效的网址 有帮助吗?

rabbitmq nestjs ts-jest
© www.soinside.com 2019 - 2024. All rights reserved.