gpt4 book ai didi

c# - 如何使用 WebApplicationFactory 播种测试数据?

转载 作者:行者123 更新时间:2023-12-05 05:02:22 27 4
gpt4 key购买 nike

我正在为 .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,我在单独的类中创建了测试。在测试类的构造函数中,我创建了一个 Client。像这样的 TestController(我在 API 中总共有 4 个 Controller ):

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 个 Controller ,我只为 3 个玩家播种。为什么我看到 12?我不明白。看起来行 db.Players.AddRange(players); 在任何 db.SaveChanges(); 之前被调用了 4 次(对于每个 Controller 测试类)。然后 db.SaveChanges(); 被连续调用了 4 次。为什么?

我的问题是:这是为什么?以及如何在集成测试中正确播种测试数据?

(如果不是通过回答,任何对一篇好文章的引用都将非常感谢)

谢谢

最佳答案

它为每个 Controller 调用 ConfigureServices。所以在这里你可以做一件事在调用 db.Database.EnsureCreated() 之前调用 db.Database.EnsureDeleted()。它将为每个 Controller 重新初始化内存数据库。

关于c# - 如何使用 WebApplicationFactory 播种测试数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62247368/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com