Override Startup in WebApplicationFactory does not map endpoints

Sam*_*amV 2 xunit.net asp.net-core identityserver4

I have configured an xUnit project to test an Identity Server implementation. I have created a TestStartup class which inherits Startup and overrides my Identity Server implementation for testing purposes. The problem is when I call my TestStartup using my custom WebApplicationFactory, my endpoints are not mapped. If I call Startup from my custom WebApplicationFactory, my endpoints are mapped.

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();
    });
}
Run Code Online (Sandbox Code Playgroud)

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);          
}
Run Code Online (Sandbox Code Playgroud)

Custom WebApplicationFactory

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

Controller Test Class

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);
    }

}
Run Code Online (Sandbox Code Playgroud)

When I run my controller test, if inject TestStartup instead of Startup, I don't get any endpoints returned from the endpoint routing, even though I call the base.Configure(app,env) method from my TestStartup class.

Hen*_*sen 7

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

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

services.AddControllers()
    .AddApplicationPart(typeof(<controllername>).Assembly);
Run Code Online (Sandbox Code Playgroud)

事情会奏效的。