![]() ![]() And another benefit is that we’re setting the actual property of our POCO in a lambda, so we’re not depending on having to specify the name of the overridden property in a string, in which we could make a typo without the compiler being able to verify it (which will be the case for all other approaches).īut this can only be used if the configuration we want to override is used with the Options pattern, and not when the configuration value is accessed directly via IConfiguration. This is a very preferable approach, because it’s simple and straightforward. Opts.OptionsConfigProperty = "OverriddenValue" Private readonly WebApplicationFactory factory įactory = new WebApplicationFactory().WithWebHostBuilder(builder =>īuilder.ConfigureTestServices(services => But the testing approach shown here works the same way if you’re using Host.CreateDefaultBuilder(args). In the Program class I like using an empty new HostBuilder() instance instead of the built-in Host.CreateDefaultBuilder(args) helper, just in order to have all the setup code explicitly visible in the repository. The integration testing strategy would be the same for more complicated projects as well, even the ones also containing HTML endpoints and cshtml files. In this example I’ll show a very basic and simple project setup for an MVC application, which will have one Json API endpoint. I tend to prefer trimming down the default generated skeletons even further, to contain only the minimal amount of necessary setup code. For the web and the test project setup I’m mostly following the patterns provided by the built-in dotnet CLI templates, so you won’t see anything surprising here. ![]() The project setupįirst I’m going to quickly describe what my project setup looks like. Hence I decided to write this down in a post for future reference. The approaches have different caveats you have to look out for, which I always tend to forget, and have to look up in old projects. There are various different ways to override the configuration for the duration of our integration tests. Disable certain features which might not be needed, or don’t work in the test environment.Replace the URL of an upstream dependency with a mock or stub.In the integration test, we might want to do the following. We can spin up our whole application with the full ASP.NET middleware pipeline in-process, with a couple of lines of code, and send HTTP requests to it for testing purposes.ĭuring the integration test, we often want to use different application configuration than what we have in the appsettings.json in our repository, which is usually tailored for local development. The pluggable and modular nature of ASP.NET Core made integration testing a much more accessible and convenient tool than it was in classic. asp.net-core testing integration-testing.Overriding configuration in ASP.NET Core integration tests ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |