gpt4 book ai didi

asp.net-core - WebApplicationFactory 中的覆盖启动不映射端点

转载 作者:行者123 更新时间:2023-12-05 00:47:06 25 4
gpt4 key购买 nike

我已经配置了一个 xUnit 项目来测试 Identity Server 的实现。我创建了一个 TestStartup继承 Startup 的类并覆盖我的 Identity Server 实现以进行测试。问题是当我调用我的TestStartup使用我的自定义 WebApplicationFactory ,我的端点没有映射。如果我调用 Startup来 self 的自定义 WebApplicationFactory ,我的端点被映射。

Startup.cs

protected readonly IConfiguration _configuration;
protected readonly IWebHostEnvironment _webHostEnvironment;

public Startup(IConfiguration configuration, IWebHostEnvironment environment)
{
_configuration = configuration;
_webHostEnvironment = environment;
}

public virtual void ConfigureServices(IServiceCollection services)
{
///code goes here
ConfigureIdentityServices(services);
}

/// <summary>
/// Split into its own method so we can override for testing
/// </summary>
/// <param name="services"></param>
public virtual void ConfigureIdentityServices(IServiceCollection services)
{
///code goes here
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public virtual void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}

app.UseIdentityServer();
app.UseRouting();

//app.UseAuthorization();

app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}

TestStartup.cs

public TestStartup(IConfiguration configuration, IWebHostEnvironment environment) : base(configuration, environment)
{

}

public override void ConfigureServices(IServiceCollection services)
{
base.ConfigureServices(services);
}

/// <summary>
/// In memory identity database implementation for testing
/// </summary>
/// <param name="services"></param>
public override void ConfigureIdentityServices(IServiceCollection services)
{
//test implementation
}

public override void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
base.Configure(app, env);
}

自定义 WebApplicationFactory

public class ApplicationFactory<TStartup> : WebApplicationFactory<TStartup> where TStartup : class
{
protected override IWebHostBuilder CreateWebHostBuilder()
{
return WebHost.CreateDefaultBuilder(null).UseEnvironment("Development")
.UseStartup<TStartup>();
}
}

Controller 测试类

public class UserControllerTests : IClassFixture<ApplicationFactory<Startup>>
{
private readonly ApplicationFactory<Startup> _factory;

public UserControllerTests(ApplicationFactory<Startup> factory)
{
_factory = factory;
}

[Fact]
public async Task Get_Users()
{
var client = _factory.CreateClient();

var result = await client.GetAsync("/user/users");
result.StatusCode.Should().Be(HttpStatusCode.OK);
}

}

当我运行 Controller 测试时,如果注入(inject) TestStartup而不是 Startup ,我没有从端点路由返回任何端点,即使我调用了 base.Configure(app,env)我的方法TestStartup类。

最佳答案

我也有同样的问题。在我的自定义 WebApplicationFactory 子类中使用子类启动会导致没有端点被注册,而 UseStartup 注册它们。奇怪的是,我有另一个 API 项目(没有身份),其中使用子类启动实际上注册了端点。尝试在启动时将所有内容都注释掉,Identity 项目中的 services.AddControllers 和 app.UseRouting/app.UseEndpoints 除外,但它仍然不起作用。

编辑:找到了解决方案。显然 services.AddControllers 只在执行程序集中注册路由,在子类集成测试场景中是测试程序集。通过添加所需的 Controller 作为应用程序部分,路由系统会选择路由:

services.AddControllers()
.AddApplicationPart(typeof(<controllername>).Assembly);

一切都会好起来的。

关于asp.net-core - WebApplicationFactory 中的覆盖启动不映射端点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58460336/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com