I'm trying to update how the company I work in initialize ASP.NET web apps and web APIs. First of all, because in our older 3.1 .NET Core setup we have no way to access a test server, and have to run integration tests through a socket on localhost, but also to try and keep our code more up to date.
Being blocked by the new magic of minimal APIs I've tried to search for how to NOT use minimal APIs, but haven't found anything that have helped me define my own Program.cs and put the code there in a way that make a test server available for unit tests.
I've used docs to set up a unit test using a test server, but when unit testing, I want to be able to create instances of objects in unit tests and supply everything I want into app init code in Program.cs. I want to be able to specify what command line arguments to use for the test. I want to be able to set a flag that makes the normal app init skip doing something. When I want to supply a mock/stub, I don't want the one running in production to be created first and then replaced.
The .NET 6 minimal code feature looks to me like an anti-feature. Why introduce new parser complexities, magic, automatic class generation and sacrifice (or obfuscate) flexibility with unit testing, just to make hello world applications save a few lines of code?
Some stuff that I have tested:
- Creating a partial Program.cs implementation doesn't work, as Program.cs can't access those members for some reason.
- There is no "this." I can use in magic Program.cs parts either. I guess the magic Program.cs exports into static contexts or hide the instance.
- There is no member in the auto-generated Program class that I've found where I can insert something on creation. I've tried to misuse services to inject stuff, but they are not available prior to "var app = builder.Build();" statement which is too late.
- If I try to debug and look into implementation of the magic Program class I'm just thrown into my minimal code which hides the details..
A static variable works, but that makes us unable to run multiple unit tests in parallel. I was hoping for a workaround by creating a static dictionary of dictionaries, and be able to send through a string key for the app to use to lookup it's test bits I want to inject, but even just passing that string key I haven't found a way to do yet, and having to implement this to begin with sounds horrible.
Some talk about app config files, but I want to be able to control this in code in unit tests and not in files, which is just a more horrific way of using static bits.
How are unit tests supposed to input a test context as input to Program.cs execution?