Dockerized integration test an ASP.NET Core app connection to RabbitMQ

I have very new to the dockerized integration testing world, so my apologies in advance if the question sounds very trivial.

I have an ASP.NET Core app that communicates with a dockerized RabbitMQ instance. When I start the application for debugging, docker-compose creates an container of that app and RabbitMQ, and then app is able to establish a connection to the RabbitMQ service. However, when running integration tests, the app fails to establish connection to the RabbitMQ service.

I do have a separate docker-compose for the integration tests that starts the RabbitMQ service, which I run it before running integration tests.

The test and application setup is fairly simple and follows a common setup, and is defined as:

public class WebAPITest : IClassFixture<WebApplicationFactory<Program>>
{
    private readonly WebApplicationFactory<Program> _factory;

    public WebAPITest(WebApplicationFactory<Program> factory)
    {
        _factory = factory;
    }

    [Fact]
    public async Task IfConsumerReceives()
    {
        var client = _factory.CreateClient();
        var response = await client.GetAsync("/api/requestitems");

        // assert
        response.EnsureSuccessStatusCode();
    }
}

The application has a RabbitMQ service, defined as it follows in the Startup class:

public void ConfigureServices(IServiceCollection services)
{
    services
        .AddMvc(options => options.EnableEndpointRouting = false)
        .SetCompatibilityVersion(CompatibilityVersion.Version_3_0);

    RegisterEventBus(services);
    services.AddOptions();
}

private void RegisterEventBus(IServiceCollection services)
{
    services.AddSingleton<IRabbitMQPersistentConnection>(sp =>
    {
        var factory = new ConnectionFactory()
        {
            HostName = Configuration["RabbitMQConnection:HostName"],
            Port = Configuration.GetValue<int>("RabbitMQConnection:Port"),
            DispatchConsumersAsync = true
        };

        factory.UserName = Configuration["RabbitMQConnection:UserName"];
        factory.Password = Configuration["RabbitMQConnection:Password"];
        var retryCount = int.Parse(Configuration["RabbitMQConnection:RetryCount"]);
        return new DefaultRabbitMQPersistentConnection(factory, logger, retryCount);
    });

    var queueName = Configuration["QueueName"];
    services.AddSingleton<IEventBus, EventBusRabbitMQ>(sp =>
    {
        var rabbitMQPersistentConnection = sp.GetRequiredService<IRabbitMQPersistentConnection>();
        var iLifetimeScope = sp.GetRequiredService<ILifetimeScope>();
        var logger = sp.GetRequiredService<ILogger<EventBusRabbitMQ>>();
        var eventBusSubcriptionsManager = sp.GetRequiredService<IEventBusSubscriptionsManager>();

        var retryCount = int.Parse(Configuration["RabbitMQConnection:RetryCount"]);

        return new EventBusRabbitMQ(rabbitMQPersistentConnection, logger, iLifetimeScope, eventBusSubcriptionsManager, queueName, retryCount);
    });

    services.AddSingleton<IEventBusSubscriptionsManager, InMemoryEventBusSubscriptionsManager>();
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    app.UseHttpsRedirection();
    app.UseMvc();
    ConfigureEventBus(app);
}

private void ConfigureEventBus(IApplicationBuilder app)
{
    // The following fails to establish connection to RabbitMQ when running integration tests.
    var eventBus = app.ApplicationServices.GetRequiredService<IEventBus>();
    eventBus.Subscribe<CollectionCompletedIntegrationEvent, CollectionCompletedIntegrationEventHandler>();
}

The error message I receive is:

RabbitMQ.Client.Exceptions.BrokerUnreachableException: ‘None of the specified endpoints were reachable’

Any suggestions is warmly appreciated.

Source: StackOverflow