Fea*_*are 10 c# xunit .net-core asp.net-core asp.net-core-3.0
我已经使用.Net Core 3.0预览版2更新了我的网站,并且希望使用TestServer 进行集成测试。在.Net Core 2.2中,我已经能够使用WebApplicationFactory<Startup>
由于WebHostBuilder即将弃用(请参阅(https://docs.microsoft.com/zh-cn/aspnet/core/migration/22-to-30?view=aspnetcore-2.2&tabs=visual-studio)了解更多详细信息。 ),我想立即跟进并实施新的泛型HostBuilder。它对于启动网站非常有用,但是在启动集成测试时崩溃。我知道WebApplicationFactory确实有用WebHostBuilder,这就是它崩溃的原因,但是我不知道如何为Generic更改它HostBuilder。
这是我在.Net Core 2.2中运行的代码:
namespace CompX.FunctionalTest.Web.Factory
{
public class CustomWebApplicationFactory<TStartup> : WebApplicationFactory<Startup>
{
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.ConfigureServices(services =>
{
// Create a new service provider.
var serviceProvider = new ServiceCollection()
.AddEntityFrameworkInMemoryDatabase()
.BuildServiceProvider();
// Add a database context (ApplicationDbContext) using an in-memory
// database for testing.
services.AddDbContext<ApplicationDbContext>(options =>
{
options.UseInMemoryDatabase("InMemoryDbForTesting");
options.UseInternalServiceProvider(serviceProvider);
});
services.AddDbContext<AppIdentityDbContext>(options =>
{
options.UseInMemoryDatabase("Identity");
options.UseInternalServiceProvider(serviceProvider);
});
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<AppIdentityDbContext>()
.AddDefaultTokenProviders();
// Build the service provider.
var sp = services.BuildServiceProvider();
// Create a scope to obtain a reference to the database
// context (ApplicationDbContext).
using (var scope = sp.CreateScope())
{
var scopedServices = scope.ServiceProvider;
var db = scopedServices.GetRequiredService<ApplicationDbContext>();
var loggerFactory = scopedServices.GetRequiredService<ILoggerFactory>();
var logger = scopedServices.GetRequiredService<ILogger<CustomWebApplicationFactory<TStartup>>>();
// Ensure the database is created.
db.Database.EnsureCreated();
try
{
// Seed the database with test data.
var userManager = scopedServices.GetRequiredService<UserManager<ApplicationUser>>();
AppIdentityDbContextSeed.SeedAsync(userManager).GetAwaiter().GetResult();
}
catch (Exception ex)
{
logger.LogError(ex, $"An error occurred seeding the database with test messages. Error: {ex.Message}");
}
}
});
}
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试使用TestServersfrom Microsoft.AspNetCore.TestHost,但是它需要new WebHostBuilder()作为参数。
我也尝试将其作为参数传递,但效果不佳:
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
Run Code Online (Sandbox Code Playgroud)
它找不到.ConfigureWebHostDefaults()功能。
是否有人成功在.Net Core 3.0中实现了测试服务器?非常感谢 !
PS:我是.Net Core的新手
编辑:
这里是 program.cs
namespace CompX.Web
{
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
}
Run Code Online (Sandbox Code Playgroud)
这是我在github上创建的问题: https : //github.com/aspnet/AspNetCore/issues/7754
我终于知道如何在 3.0 中做到这一点。这是有关如何为需要解决方案的任何人执行此操作的完整演练:
您至少需要.Net Core 3.0.0-preview2,因为他们在此预览版中添加了WebApplicationFactorywith IHostbuilder( https://github.com/aspnet/AspNetCore/pull/6585 )。你可以在这里找到它:https : //dotnet.microsoft.com/download/dotnet-core/3.0
至少将这些包升级到 3.0.0 版(https://github.com/aspnet/AspNetCore/issues/3756和https://github.com/aspnet/AspNetCore/issues/3755):
Microsoft.AspNetCore.App Version=3.0.0-preview-19075-0444Microsoft.AspNetCore.Mvc.Testing Version= 3.0.0-preview-19075-0444Microsoft.Extensions.Hosting Version=3.0.0-preview.19074.2删除现已弃用的软件包,这些软件包现在包含在Microsoft.AspNetCore.App:
Microsoft.AspNetCore Version=2.2.0Microsoft.AspNetCore.CookiePolicy Version=2.2.0Microsoft.AspNetCore.HttpsPolicy Version=2.2.0Microsoft.AspNetCore.Identity Version=2.2.0如果您services.AddIdentity在WebApplicationFactory<Startup>构建器中使用,则需要将其删除。否则,您将收到一个新错误,说明您已经为Identity.Application. 看起来新WebApplicationFactory的从Startup.cs现在开始使用。
我没有其他要修改的。希望对一些人有所帮助!
更新 :
它运行良好,直到我不得不使用另一个集成 C# 文件(例如LoginTest.cs和ManageTest.cs)。问题是当我运行我的测试时,它会无限循环,直到我按下 CTRL + C。之后,它会显示拒绝访问错误。
再一次,我不得不从我WebApplicationFactory的种子中删除一些东西:
try
{
// Seed the database with test data.
var userManager = scopedServices.GetRequiredService<UserManager<ApplicationUser>>();
AppIdentityDbContextSeed.SeedAsync(userManager).GetAwaiter().GetResult();
}
catch (Exception ex)
{
logger.LogError(ex, $"An error occurred seeding the database with test messages. Error: {ex.Message}");
}
Run Code Online (Sandbox Code Playgroud)
看起来新的WebApplicationFactory试图为每个工厂重新创建用户管理器。我用 Guid.NewGuid() 替换了我的种子用户帐户。
我花了一段时间才弄明白。希望它可以再次帮助某人。
| 归档时间: |
|
| 查看次数: |
1327 次 |
| 最近记录: |