title: Configure MassTransit for testing with WebApplicationFactory<Startup>
body: <p>I have an ASP.NET Core web app and test setup using <code>WebApplicationFactory</code> to test my controller actions. I used RawRabbit before and it was easy enough for me to mock the <code>IBusClient</code> and add it to the DI container as a singleton. Within the <code>WebApplicationFactory&lt;TStartup&gt;.CreateWebHostBuilder()</code> I call this extension method to add my mocked <code>IBusClient</code> instance like so;</p>
  
  
  
  <pre class="lang-cs prettyprint-override"><code>/// &lt;summary&gt;
  /// Configures the service bus.
  /// &lt;/summary&gt;
  /// &lt;param name="webHostBuilder"&gt;The web host builder.&lt;/param&gt;
  /// &lt;returns&gt;A web host builder.&lt;/returns&gt;
  public static IWebHostBuilder ConfigureTestServiceBus(this IWebHostBuilder webHostBuilder)
  {
      webHostBuilder.ConfigureTestServices(services =&gt;
      {
          services.AddSingleton&lt;IBusClient, MY_MOCK_INSTANCE&gt;
      });
  
      return webHostBuilder;
  }
  </code></pre>
  
  <p>But there are gaps in RawRabbit right now that made me decide to move over to MassTransit. However, I am wondering if there's already a better way to register the <code>IBus</code> into my container without mocking it inside my test. Not sure if <code>InMemoryTestFixture</code>, <code>BusTestFixture</code>, or <code>BusTestHarness</code> is the solution to my problem. Not sure how to use them together and what they do.</p>
  
  <p>By the way, in my ASP.NET Core app, I have a reusable extension method setup like the code below to hook me up to RabbitMQ on startup.</p>
  
  <pre class="lang-cs prettyprint-override"><code>/// &lt;summary&gt;
  /// Adds the service bus.
  /// &lt;/summary&gt;
  /// &lt;param name="services"&gt;The services.&lt;/param&gt;
  /// &lt;param name="configurator"&gt;The configurator.&lt;/param&gt;
  /// &lt;returns&gt;A service collection.&lt;/returns&gt;
  public static IServiceCollection AddServiceBus(this IServiceCollection services, Action&lt;IServiceCollectionConfigurator&gt; configurator)
  {
      var rabbitMqConfig = new ConfigurationBuilder()
          .AddJsonFile("/app/configs/service-bus.json", optional: false, reloadOnChange: true)
          .Build();
  
      // Setup DI for MassTransit.
      services.AddMassTransit(x =&gt;
      {
          configurator(x);
  
          // Get the json configuration and use it to setup connection to RabbitMQ.
          var rabbitMQConfig = rabbitMqConfig.GetSection(ServiceBusOptionsKey).Get&lt;RabbitMQOptions&gt;();
  
          // Add bus to the container.
          x.AddBus(provider =&gt; Bus.Factory.CreateUsingRabbitMq(cfg =&gt;
          {
              cfg.Host(
                  new Uri(rabbitMQConfig.Host),
                  hostConfig =&gt;
                  {
                      hostConfig.Username(rabbitMQConfig.Username);
                      hostConfig.Password(rabbitMQConfig.Password);
                      hostConfig.Heartbeat(rabbitMQConfig.Heartbeat);
                  });
  
              cfg.ConfigureEndpoints(provider);
  
              // Add Serilog logging.
              cfg.UseSerilog();
          }));
      });
  
      // Add the hosted service that starts and stops the BusControl.
      services.AddSingleton&lt;IMessageDataRepository, EncryptedMessageDataRepository&gt;();
      services.AddSingleton&lt;IEndpointNameFormatter, EndpointNameFormatter&gt;();
      services.AddSingleton&lt;IBus&gt;(provider =&gt; provider.GetRequiredService&lt;IBusControl&gt;());
      services.AddSingleton&lt;IHostedService, BusHostedService&gt;();
  
      return services;
  }
  </code></pre>