admin管理员组文章数量:1196352
I am trying to integration test a whole Web-API Project. When starting the web-api using 'assembly.exe or using console 'dotnet assembly.dll' the api starts successfully and all endpoints are discovered. But when I execute the Program.Main() from a unit test no endpoints are configured during startup although the same code is executed. Does anybody have a clue what is different between standalone execution and execution from a unit test:
[OneTimeSetUp]
public static void StartWebApiForLocalRuns()
{
Program.Main().ConfigureAwait(false);
}
public static async Task Main()
{
var builder = WebApplication.CreateBuilder();
builder.Configuration
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile("appsettings.logging.json", optional: false, reloadOnChange: true)
.AddEnvironmentVariables();
builder.Host
.UseWindowsService()
.UseSerilog();
Startup.ConfigureServices(builder.Configuration, builder.Services);
var app = builder.Build();
Startup.Configure(_app, builder.Environment, _app.Lifetime, _app.Services);
await app.RunAsync();
}
When running the Web-App from VS:
Endpoints State from normal start
but running from unit test:
Endpoints State from unit test start
I am trying to integration test a whole Web-API Project. When starting the web-api using 'assembly.exe or using console 'dotnet assembly.dll' the api starts successfully and all endpoints are discovered. But when I execute the Program.Main() from a unit test no endpoints are configured during startup although the same code is executed. Does anybody have a clue what is different between standalone execution and execution from a unit test:
[OneTimeSetUp]
public static void StartWebApiForLocalRuns()
{
Program.Main().ConfigureAwait(false);
}
public static async Task Main()
{
var builder = WebApplication.CreateBuilder();
builder.Configuration
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile("appsettings.logging.json", optional: false, reloadOnChange: true)
.AddEnvironmentVariables();
builder.Host
.UseWindowsService()
.UseSerilog();
Startup.ConfigureServices(builder.Configuration, builder.Services);
var app = builder.Build();
Startup.Configure(_app, builder.Environment, _app.Lifetime, _app.Services);
await app.RunAsync();
}
When running the Web-App from VS:
Endpoints State from normal start
but running from unit test:
Endpoints State from unit test start
Share Improve this question edited Jan 23 at 13:33 ath0m asked Jan 22 at 22:58 ath0math0m 12 bronze badges1 Answer
Reset to default 0I solved it by manually setting the application name to the default namespace of the web application. As when starting the application from a unit test the application name per default changes to "testhost". This breaks the reflection based search for controllers as .net will not search for controllers referenced by the applications default assembly. By forcing the application name to the default assembly name of the web-api all controllers are loaded as expected. This simplifies the (integration-testing) as we can simply start the application using Program.Main().
Also found a solution reading this post: Integration tests do not recognize actions of controllers
var options = new WebApplicationOptions()
{
ApplicationName = typeof(Program).Assembly.GetName().Name //important to allow starting API from unit test
};
var builder = WebApplication.CreateBuilder(options);
本文标签: cNo Endpoints are configured when ProgramMain() is called from an unit testStack Overflow
版权声明:本文标题:c# - No Endpoints are configured when Program.Main() is called from an unit test - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738534538a2094262.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论