如何使用 WebApplicationFactory 播种测试数据?
How to seed test data using WebApplicationFactory?
我正在为 .NET Core 3.1 Web API
进行集成测试。我关注了这篇文章Painless Integration Testing with ASP.NET Core Web API。这是我的 CustomWebApplicationFactory
:
public class CustomWebApplicationFactory<TStartup> : WebApplicationFactory<TStartup> where TStartup : class
{
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.ConfigureServices(services =>
{
var descriptor = services.SingleOrDefault(d => d.ServiceType == typeof(DbContextOptions<TestDbContext>));
if (descriptor != null)
{
services.Remove(descriptor);
}
services.AddDbContext<TestDbContext>(options =>
{
options.UseInMemoryDatabase("InMemoryTestDb");
});
var sp = services.BuildServiceProvider();
using (var scope = sp.CreateScope())
{
var scopedServices = scope.ServiceProvider;
var db = scopedServices.GetRequiredService<TestDbContext>();
var logger = scopedServices.GetRequiredService<ILogger<CustomWebApplicationFactory<TStartup>>>();
db.Database.EnsureCreated();
try
{
Console.WriteLine("Seeding...");
var players = Utilities.GetTestPlayers(); // Returns 3 test players
db.Players.RemoveRange(db.Players); // Clear the table
db.Players.AddRange(players); // Add 3 test players
db.SaveChanges();
Console.WriteLine($"Players seeded: { db.Players.Count() }");
}
catch (Exception ex)
{
logger.LogError(ex, "An error occurred seeding the " + "database with test messages. Error: {Message}", ex.Message);
}
}
});
}
}
这些是我的测试: 对于每个 Controller
我在单独的 class 中创建了测试。在测试 class 的构造函数中,我创建了一个 Client
。像这样 TestController
(我在 API 中总共有 4 个控制器):
public class BaseControllerTest : IClassFixture<CustomWebApplicationFactory<Startup>>
{
protected HttpClient Client { get; }
public BaseControllerTest(CustomWebApplicationFactory<Startup> factory)
{
Client = factory.CreateClient();
}
}
public class TestControllerShould : BaseControllerTest
{
public TestControllerShould(CustomWebApplicationFactory<Startup> factory)
: base(factory)
{
Console.WriteLine("TestControllerShould");
}
[Fact]
public async Task GetHelloWorld()
{
var request = new HttpRequestMessage(new HttpMethod("GET"), "/test/");
var response = await Client.SendAsync(request);
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
var content = await response.Content.ReadAsStringAsync();
Assert.Equal("Hello World!", content);
Assert.False(response.Headers.Contains("Server"), "Should not contain server header");
}
}
我的问题是:
正如您在 CustomWebApplicationFactory
中看到的,我在播种时记录,这是我看到的日志:
Seeding...
Seeding...
Seeding...
Seeding...
Players seeded: 12
Players seeded: 12
Players seeded: 12
Players seeded: 12
我总共有 4 个控制器,我只为 3 个玩家播种。为什么我看到 12?我不明白。看起来行 db.Players.AddRange(players);
在任何 db.SaveChanges();
之前被调用了 4 次(对于每个控制器测试 class)。然后 db.SaveChanges();
被连续调用了 4 次。为什么?
我的问题是:这是为什么?以及如何在集成测试中正确播种测试数据?
(如果不是通过回答,任何对一篇好文章的引用都将非常感谢)
谢谢
它为每个控制器调用 ConfigureServices
。
所以在这里你可以做一件事
在调用 db.Database.EnsureCreated()
之前调用 db.Database.EnsureDeleted()
。
它将为每个控制器重新初始化内存数据库。
我正在为 .NET Core 3.1 Web API
进行集成测试。我关注了这篇文章Painless Integration Testing with ASP.NET Core Web API。这是我的 CustomWebApplicationFactory
:
public class CustomWebApplicationFactory<TStartup> : WebApplicationFactory<TStartup> where TStartup : class
{
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.ConfigureServices(services =>
{
var descriptor = services.SingleOrDefault(d => d.ServiceType == typeof(DbContextOptions<TestDbContext>));
if (descriptor != null)
{
services.Remove(descriptor);
}
services.AddDbContext<TestDbContext>(options =>
{
options.UseInMemoryDatabase("InMemoryTestDb");
});
var sp = services.BuildServiceProvider();
using (var scope = sp.CreateScope())
{
var scopedServices = scope.ServiceProvider;
var db = scopedServices.GetRequiredService<TestDbContext>();
var logger = scopedServices.GetRequiredService<ILogger<CustomWebApplicationFactory<TStartup>>>();
db.Database.EnsureCreated();
try
{
Console.WriteLine("Seeding...");
var players = Utilities.GetTestPlayers(); // Returns 3 test players
db.Players.RemoveRange(db.Players); // Clear the table
db.Players.AddRange(players); // Add 3 test players
db.SaveChanges();
Console.WriteLine($"Players seeded: { db.Players.Count() }");
}
catch (Exception ex)
{
logger.LogError(ex, "An error occurred seeding the " + "database with test messages. Error: {Message}", ex.Message);
}
}
});
}
}
这些是我的测试: 对于每个 Controller
我在单独的 class 中创建了测试。在测试 class 的构造函数中,我创建了一个 Client
。像这样 TestController
(我在 API 中总共有 4 个控制器):
public class BaseControllerTest : IClassFixture<CustomWebApplicationFactory<Startup>>
{
protected HttpClient Client { get; }
public BaseControllerTest(CustomWebApplicationFactory<Startup> factory)
{
Client = factory.CreateClient();
}
}
public class TestControllerShould : BaseControllerTest
{
public TestControllerShould(CustomWebApplicationFactory<Startup> factory)
: base(factory)
{
Console.WriteLine("TestControllerShould");
}
[Fact]
public async Task GetHelloWorld()
{
var request = new HttpRequestMessage(new HttpMethod("GET"), "/test/");
var response = await Client.SendAsync(request);
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
var content = await response.Content.ReadAsStringAsync();
Assert.Equal("Hello World!", content);
Assert.False(response.Headers.Contains("Server"), "Should not contain server header");
}
}
我的问题是:
正如您在 CustomWebApplicationFactory
中看到的,我在播种时记录,这是我看到的日志:
Seeding...
Seeding...
Seeding...
Seeding...
Players seeded: 12
Players seeded: 12
Players seeded: 12
Players seeded: 12
我总共有 4 个控制器,我只为 3 个玩家播种。为什么我看到 12?我不明白。看起来行 db.Players.AddRange(players);
在任何 db.SaveChanges();
之前被调用了 4 次(对于每个控制器测试 class)。然后 db.SaveChanges();
被连续调用了 4 次。为什么?
我的问题是:这是为什么?以及如何在集成测试中正确播种测试数据?
(如果不是通过回答,任何对一篇好文章的引用都将非常感谢)
谢谢
它为每个控制器调用 ConfigureServices
。
所以在这里你可以做一件事
在调用 db.Database.EnsureCreated()
之前调用 db.Database.EnsureDeleted()
。
它将为每个控制器重新初始化内存数据库。