You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have an ASP.Net application where the Program.Main method is doing this:
publicclassProgram{publicstaticasync Task Main(string[]args){if(!ArgumentsAreOk(args))return;// do all the rest of the initialization}}
To make sure things are working properly, I'm trying to do integration tests by using Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory for this Program class with code like this:
The ArgumentsAreOk method is quite finicky about what it receives and I'd rather have not to change it.
Sadly in the context of the test code above, the Program.Main method receives an array of multiple parameters, like so:
The first is named as an environment variable defined on the machine while the others are apparently coming "out of the blue".
I looked around for an explanation as to why I get those arguments and found that line in the sources but I can't figure out a solution to clear the _hostConfiguration instance.
I have tried to add the following code in the WithWebHostBuilder lambda:
But despite the code being called, I still have arguments being given to Program.Main
I know that I could change the SUT behavior to accommodate for the unexpected arguments, but it feels wrong to have to change the code to adapt to the testing environment.
Taking a different approach, I figured out that it's the host factory that receives the arguments.
So changing the default one to one of my own might help me solve the situation, but it's created by a call to HostFactoryResolver.ResolveHostFactory as seen on this line and I'm not quite sure how to "plug" myself in it.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hello,
I have an ASP.Net application where the Program.Main method is doing this:
To make sure things are working properly, I'm trying to do integration tests by using
Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory
for thisProgram
class with code like this:The
ArgumentsAreOk
method is quite finicky about what it receives and I'd rather have not to change it.Sadly in the context of the test code above, the
Program.Main
method receives an array of multiple parameters, like so:The first is named as an environment variable defined on the machine while the others are apparently coming "out of the blue".
I looked around for an explanation as to why I get those arguments and found that line in the sources but I can't figure out a solution to clear the
_hostConfiguration
instance.I have tried to add the following code in the
WithWebHostBuilder
lambda:but it did not change anything, which is even confirmed by the fact that a breakpoint on
Sources.Clear()
is not reached beforeProgram.Main
is called.I also tried adding the following:
But despite the code being called, I still have arguments being given to
Program.Main
I know that I could change the SUT behavior to accommodate for the unexpected arguments, but it feels wrong to have to change the code to adapt to the testing environment.
Taking a different approach, I figured out that it's the host factory that receives the arguments.
So changing the default one to one of my own might help me solve the situation, but it's created by a call to
HostFactoryResolver.ResolveHostFactory
as seen on this line and I'm not quite sure how to "plug" myself in it.Any suggestions are most welcome.
Beta Was this translation helpful? Give feedback.
All reactions