为ASP.NET Core / EF Core / xUnit.NET集成测试中的每个测试种子测试数据

时间:2019-04-23 12:22:49

标签: c# asp.net asp.net-core integration-testing xunit.net

我一直在遵循使用Integration tests in ASP.NET Core上的Microsoft文档为ASP.NET Core 2.2 API设置测试的策略。

总而言之,我们扩展并自定义WebApplicationFactory,并使用IWebHostBuilder来设置和配置各种服务,以便使用内存数据库为我们提供数据库上下文,以进行以下测试(复制和粘贴)摘自文章):

public class CustomWebApplicationFactory<TStartup> 
    : WebApplicationFactory<TStartup> where TStartup: class
{
    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);
            });

            // 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 logger = scopedServices
                    .GetRequiredService<ILogger<CustomWebApplicationFactory<TStartup>>>();

                // Ensure the database is created.
                db.Database.EnsureCreated();

                try
                {
                    // Seed the database with test data.
                    Utilities.InitializeDbForTests(db);
                }
                catch (Exception ex)
                {
                    logger.LogError(ex, $"An error occurred seeding the " +
                        "database with test messages. Error: {ex.Message}");
                }
            }
        });
    }
}

在测试中,我们可以使用工厂来创建客户端,如下所示:

public class IndexPageTests : 
    IClassFixture<CustomWebApplicationFactory<RazorPagesProject.Startup>>
{
    private readonly HttpClient _client;
    private readonly CustomWebApplicationFactory<RazorPagesProject.Startup> 
        _factory;

    public IndexPageTests(
        CustomWebApplicationFactory<RazorPagesProject.Startup> factory)
    {
        _factory = factory;
        _client = factory.CreateClient(new WebApplicationFactoryClientOptions
            {
                AllowAutoRedirect = false
            });
    }

    [Fact]
    public async Task Test1()
    {
        var response = await _client.GetAsync("/api/someendpoint");
    }
}

这很好,但是请注意对InitializeDbForTests的调用,该调用在配置服务后为所有测试设置了一些测试数据。

我想要一个合理的策略,以干净的方式开始每个API测试,以使测试不会相互依赖。我一直在寻找各种方法来使测试方法中的ApplicationDbContext无效。

进行完全相互隔离的集成测试是否合理,我该如何使用ASP.NET Core / EF Core / xUnit.NET进行集成测试?

3 个答案:

答案 0 :(得分:0)

具有讽刺意味的是,您正在寻找EnsureDeleted而不是EnsureCreated。那将转储数据库。由于内存中的“数据库”是无模式的,因此您实际上不需要确保已创建它甚至可以迁移它。

此外,您不应在内存数据库中使用硬编码名称。实际上,这将导致同一数据库实例在内存中随处使用。相反,您应该随机使用一些东西:Guid.NewGuid().ToString()足够好。

答案 1 :(得分:0)

实际上,Testing with InMemory在标题为“笔试”的部分中很好地描述了该过程。这是一些说明基本思想的代码

    [TestClass]
public class BlogServiceTests
{
    [TestMethod]
    public void Add_writes_to_database()
    {
        var options = new DbContextOptionsBuilder<BloggingContext>()
            .UseInMemoryDatabase(databaseName: "Add_writes_to_database")
            .Options;

这个想法是每个测试方法都有一个单独的数据库,因此您不必担心测试运行的顺序或它们并行运行的事实。当然,您必须添加一些代码来填充您的数据库,并从每个测试方法中调用它。

我已经使用了这种技术,并且效果很好。

答案 2 :(得分:0)

好的,这样我就可以了!获取作用域服务是关键。当我想从头开始播种时,可以通过将播种调用包装在using (var scope = _factory.Server.Host.Services.CreateScope()) { }部分中来开始每个测试,在该部分中,我可以先var scopedServices = scope.ServiceProvider;然后在var db = scopedServices.GetRequiredService<MyDbContext>();之前db.Database.EnsureDeleted(),最后运行我的播种功能。有点笨拙,但可以。

感谢克里斯·普拉特(Chris Pratt)的帮助(来自评论的答案)。

相关问题