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:
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:
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:
That's it!