Create and running tests- part 4
WebAPI2CLIThis is a part of the series where about how I made the WebAPI2CLI - Execute ASP.NET Core WebAPI from Command Line
Source code on https://github.com/ignatandrei/webAPI2CLI/
|1||WebAPI2CLI - Description|
|3||WebAPI2CLI - implementing|
|4||WebAPI2CLI - tests|
|5||WebAPI2CLI - Devops and CI/CD|
|6||WebAPI2CLI - documentation|
|7||WebAPI2CLI - Conclusions|
|8||WebAPI2CLI - Zip application|
I have made a TestWebSite , in .NET Core 3.1, I was also thinking that WebApplicationFactory exists and tutorial like https://docs.microsoft.com/en-us/aspnet/core/test/integration-tests?view=aspnetcore-3.1 it will be easy to test the site.
However, I have found the hard way that is not a real server : it does not have the adresses / ports open. Read https://github.com/dotnet/aspnetcore/issues/4892 .
So I have made a hack – see public class LocalServerFactory in the source code.
Also, WithWebHostBuilder helps create a new WebApplicationFactory -used to test with / or without / CLI_ENABLED command line
Second point was testing methods like
Because it is an extension function, I cannot mock. So I refactored the code to
public bool ShouldShowHelp()
//deleted for easy of automating testing
//var showHelp = configuration.GetValue<int?>(“CLI_HELP”, null);
var cliHelp = configuration[“CLI_HELP”];
var showHelp = int.TryParse(cliHelp, out var val) && val == 1;
Also, I have resorted to XBehave, to have tests written in easy form. And to Moq to mock various properties.