CustomWebApplicationFactory不支持';将ASP.Net从2.2迁移到3.0时无法正常工作
当我将项目从ASP.NET Core 2.2迁移到3.0时,我的auth UnitTests失败。 我不知道在迁移时是否需要修改CustomWebApplicationFactory,我在ASP.NET教程中没有找到它的信息。请帮我弄清楚。谢谢 这是我的密码:CustomWebApplicationFactory不支持';将ASP.Net从2.2迁移到3.0时无法正常工作,asp.net,migration,Asp.net,Migration,当我将项目从ASP.NET Core 2.2迁移到3.0时,我的auth UnitTests失败。 我不知道在迁移时是否需要修改CustomWebApplicationFactory,我在ASP.NET教程中没有找到它的信息。请帮我弄清楚。谢谢 这是我的密码: UserControllerAuthTests: [TestMethod] public async Task UserController_No_Auth_Should_Fail()
UserControllerAuthTests:
[TestMethod]
public async Task UserController_No_Auth_Should_Fail()
{
// Arrange
var client = new CustomWebApplicationFactory().CreateClientWithoutTestAuth();
// Act
var response = await client.GetAsync("User/Index");
// Assert
Assert.AreEqual(HttpStatusCode.Redirect, response.StatusCode);
// change it to Contains "Identity/Account/Login" instead of StartsWith
Assert.IsTrue(response.Headers.Location.OriginalString.Contains("http://localhost/Identity/Account/Login"));
client.Dispose();
}
我的CustomWebApplicationFactory:
CustomWebApplicationFactory:
public class CustomWebApplicationFactory : WebApplicationFactory<MockStartup>
{
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
base.ConfigureWebHost(builder);
}
protected override IWebHostBuilder CreateWebHostBuilder()
{
return WebHost.CreateDefaultBuilder()
.UseStartup<MockStartup>();
}
}
CustomWebApplicationFactory:
公共类CustomWebApplicationFactory:WebApplicationFactory
{
受保护的覆盖无效配置WebHost(IWebHostBuilder)
{
base.ConfigureWebHost(构建器);
}
受保护的覆盖IWebHostBuilder CreateWebHostBuilder()
{
返回WebHost.CreateDefaultBuilder()
.UseStartup();
}
}
帮助功能:
public static class WebApplicationFactoryExtensions
{
// Method which adds TestAuthHandler to AuthenticationBuilder
public static AuthenticationBuilder AddTestAuth(this AuthenticationBuilder builder, Action<AuthenticationSchemeOptions> configureOptions)
{
return builder.AddScheme<AuthenticationSchemeOptions, TestAuthHandler>("Test", "Test Auth", configureOptions);
}
// Method which added Authentication to factory using ConfigureTestServices.
// We provide TestClaimsProvider as parameter which is adde to services.
public static WebApplicationFactory<T> WithAuthentication<T>(this WebApplicationFactory<T> factory, TestClaimsProvider claimsProvider) where T : class
{
return factory.WithWebHostBuilder(builder =>
{
builder.ConfigureTestServices(services =>
{
// Schema name must match with Schema added to AuthenticationBuilder
services.AddAuthentication(options => {
options.DefaultAuthenticateScheme = "Test";
options.DefaultChallengeScheme = "Test";
}).AddTestAuth(o => { });
services.AddScoped<TestClaimsProvider>(_ => claimsProvider);
});
});
}
// Method used to create a mock client with authentication based on providing Claims.
public static HttpClient CreateClientWithTestAuth<T>(this WebApplicationFactory<T> factory, TestClaimsProvider claimsProvider) where T : class
{
var client = factory.WithAuthentication(claimsProvider).CreateClient(new WebApplicationFactoryClientOptions
{
AllowAutoRedirect = false
});
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Test");
return client;
}
// Method used to create mock client without any authentication.
public static HttpClient CreateClientWithoutTestAuth<T>(this WebApplicationFactory<T> factory) where T : class
{
var client = factory.CreateClient(new WebApplicationFactoryClientOptions
{
AllowAutoRedirect = false
});
return client;
}
}
公共静态类WebApplicationFactoryExtensions
{
//将TestAuthHandler添加到AuthenticationBuilder的方法
公共静态AuthenticationBuilder AddTestAuth(此AuthenticationBuilder,操作配置选项)
{
返回builder.AddScheme(“测试”、“测试验证”、配置选项);
}
//方法,该方法使用ConfigureTestServices向工厂添加身份验证。
//我们提供TestClaimsProvider作为参数添加到服务中。
带有身份验证的公共静态WebApplicationFactory(此WebApplicationFactory,TestClaimsProvider claimsProvider),其中T:class
{
返回factory.WithWebHostBuilder(builder=>
{
builder.ConfigureTestServices(服务=>
{
//架构名称必须与添加到AuthenticationBuilder的架构匹配
services.AddAuthentication(选项=>{
options.DefaultAuthenticateScheme=“测试”;
options.DefaultChallengeScheme=“测试”;
}).AddTestAuth(o=>{});
services.AddScoped(=>claimsProvider);
});
});
}
//用于创建具有基于提供声明的身份验证的模拟客户端的方法。
公共静态HttpClient CreateClientWithTestAuth(此WebApplicationFactory工厂,TestClaimsProvider claimsProvider),其中T:class
{
var client=factory.WithAuthentication(claimsProvider).CreateClient(新WebApplicationFactoryClientOptions
{
AllowAutoRedirect=false
});
client.DefaultRequestHeaders.Authorization=新的AuthenticationHeaderValue(“测试”);
返回客户;
}
//用于创建模拟客户端而不进行任何身份验证的方法。
公共静态HttpClient CreateClient不带TestAuth(此WebApplicationFactory工厂),其中T:class
{
var client=factory.CreateClient(新的WebApplicationFactoryClientOptions
{
AllowAutoRedirect=false
});
返回客户;
}
}
测试结果:
Assert.AreEqual failed. Expected:<Redirect>. Actual:<NotFound>.
Assert.AreEqual失败。预期:。实际值:。
我想知道customWebApplicationFactory设置是否有问题