When monitoring our RabbitMQ usage, I noticed some strange behavior. After drilling a little bit deeper in our monitoring data, I discovered that the integration tests for one of our projects were running against our production environment! Whoops!!
Luckily the impact was limited but that doesn't mean we don't have to fix it.
The fix
Let me show you how we did it…
The unit test code was using the Microsoft.AspNetCore.Mvc.Testing library to create an in-memory test host to test an API. Microsoft.AspNetCore.Mvc.Testing is part of the ASP.NET Core ecosystem and aims to simplify integration testing by providing essential tools and setup.
Here is the original code:
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Net.Http; | |
using System.Text; | |
using System.Threading.Tasks; | |
using Microsoft.AspNetCore.Hosting; | |
using Microsoft.AspNetCore.Mvc.Testing; | |
using Microsoft.AspNetCore.TestHost; | |
using Microsoft.Extensions.Configuration; | |
using Microsoft.Extensions.DependencyInjection; | |
using Microsoft.Extensions.Hosting; | |
using NSubstitute; | |
namespace DocumentGeneration.API.Tests; | |
public sealed class SelfHostedApi : WebApplicationFactory<Startup> | |
{ | |
public DocumentApi DocumentApi { get; } | |
public HttpClient DocumentClient { get; } | |
public SelfHostedApi() | |
{ | |
DocumentApi = new DocumentApi(); | |
DocumentClient = DocumentApi.CreateClient(); | |
} | |
protected override IHostBuilder CreateHostBuilder() | |
{ | |
return Host | |
.CreateDefaultBuilder() | |
.ConfigureWebHostDefaults(builder => builder.UseStartup<Startup>()); | |
} | |
protected override void ConfigureWebHost(IWebHostBuilder builder) | |
{ | |
builder.ConfigureServices(services => | |
{ | |
var httpClientFactory = Substitute.For<IHttpClientFactory>(); | |
httpClientFactory.CreateClient("").ReturnsForAnyArgs(DocumentClient); | |
services.AddTransient<IHttpClientFactory>(s => httpClientFactory); | |
}); | |
} | |
protected override void Dispose(bool disposing) | |
{ | |
DocumentApi.Dispose(); | |
base.Dispose(disposing); | |
} | |
} |
There is nothing wrong with the code itself and it makes it very easy to write and end-to-end integration test against our web app:
public class ConversionApiTests: IClassFixture<SelfHostedApi> | |
{ | |
public HttpClient Sut { get; } | |
public ConversionApiTests(SelfHostedApi fixture) | |
{ | |
Sut = fixture.CreateClient(); | |
} | |
[Fact] | |
public async Task API_Converts_Ûploaded_File() | |
{ | |
//Arrange | |
var sourceFile ="ExampleDocument.docx"; | |
var targetFile = $"ExampleDocument-{Guid.NewGuid()}.pdf"; | |
var form = new MultipartFormBuilder() | |
.AddFile(sourceFile,name:"file") | |
.Build(); | |
//Act | |
var response = await Sut.PostAsync("/convert/upload/docx/to/ExamplePDF.pdf", form); | |
await response.WriteToFile(targetFile); | |
//Assert | |
Assert.True(File.Exists(targetFile)); | |
} | |
} |
One of the api endpoints puts messages on a RabbitMQ queue to process them later. A test instance exists but as I mentioned at the beginning of this post, instead of using the ‘test’ settings found in appsettings.test.json, it was using the production settings.
To fix it I had to update the code to explicitly set the environment:
public sealed class SelfHostedApi : WebApplicationFactory<Startup> | |
{ | |
public DocumentApi DocumentApi { get; } | |
public HttpClient DocumentClient { get; } | |
public SelfHostedApi() | |
{ | |
DocumentApi = new DocumentApi(); | |
DocumentClient = DocumentApi.CreateClient(); | |
} | |
protected override IHostBuilder CreateHostBuilder() | |
{ | |
return Host | |
//Add the line below 👇 to explicitly set the environment used | |
.UseEnvironment("test") | |
.CreateDefaultBuilder() | |
.ConfigureWebHostDefaults(builder => builder.UseStartup<Startup>()); | |
} | |
} |
That's it!