仅将数据添加到DbContext一次

问题描述 投票:0回答:1

我创建了一个XUnit固定装置来定义EF Core上下文初始数据:

public class ServiceProviderFixture : IDisposable {

  public IServiceProvider Provider { get; private set; }

  public ServiceProviderFixture() {
    IServiceCollection services = new ServiceCollection();
    services.AddDbContext<Context>(x => { x.UseInMemoryDatabase("Database"); });
    Provider = services.BuildServiceProvider();
    BuildContext();
  }

  private void BuildContext() { 
    Context context = Provider.GetService<Context>();
    context.Countries.Add(new Country { Code = "fr", Name = "France" });
    context.SaveChanges();
  }

  public void Dispose() { } 

} 

然后在一些测试中,我按如下方式使用它:

 public class TestMethod1 : IClassFixture<ServiceProviderFixture> {

   public Test(ServiceProviderFixture fixture) {
    _fixture = fixture;
   } 

  [Fact]
  public async Task Test1() {

    IServiceProvider provider = _fixture.Provider;

    Context context = provider.GetService<Context>();

    // Add test data to context
    // Test some method

  }

} 

[当我运行一个测试时,效果很好...但是当我使用dotnet test运行所有测试时,我得到了:

An item with the same key has already been added. Key: fr
The following constructor parameters did not have matching fixture data:
ServiceProviderFixture fixture)

我相信在同一上下文中,每个TestClass被调用BuildContext()一次。

我该如何解决?

c# asp.net-core entity-framework-core xunit xunit.net
1个回答
0
投票

只要检查您的BuildContext中是否有任何数据,否则就什么也不做。或者,您也可以在测试完成后清除创建的数据。

  private void BuildContext() { 
    Context context = Provider.GetService<Context>();
    if(!context.Countries.Any())
    {
        context.Countries.Add(new Country { Code = "fr", Name = "France" });
        context.SaveChanges();
    }
  }

0
投票

因为您总是以相同的方式命名内存数据库,所以您总是会再次获得相同的数据库。对于每个测试用例,您都必须使用不同的名称(例如Guid.NewGuid().ToString())。

© www.soinside.com 2019 - 2024. All rights reserved.