WebApplicationFactory中的重写启动不会映射端点

问题描述 投票:0回答:1

我已经配置了一个xUnit项目来测试Identity Server实现。我创建了一个TestStartup类,该类继承了Startup并覆盖了我的Identity Server实现以进行测试。问题是当我使用自定义TestStartup调用WebApplicationFactory时,未映射端点。如果我从自定义Startup调用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>();
    }
}

控制器测试类

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

}

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

我已经配置了一个xUnit项目来测试Identity Server实现。我已经创建了一个TestStartup类,该类继承了Startup并覆盖了我的Identity Server实现以进行测试...

asp.net-core identityserver4 xunit.net
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.