[英]NUnit Entity Framework integration test dependency injection issue with .NET Core
我剛剛開始研究帶有實體框架的.NET Core。 我以前將.NET Framework與Ninject一起使用,但現在嘗試使用.NET Core中內置的DI。
我有一個TestBase
類,我的測試將源自該類。 我希望此類負責使用[OneTimeSetUp]
和[OneTimeTearDown]
創建和刪除測試數據庫。 問題是我似乎無法弄清楚如何在設置和拆卸方法中訪問我的DI服務。 這些方法不能具有參數,而我的TestBase
類必須具有無參數的構造函數,因此我也無法從那里獲得它們。
[SetUpFixture]
public partial class TestBase
{
protected IEFDatabaseContext DataContext { get; set; }
public TestBase(IEFDatabaseContext dataContext)
{
this.DataContext = dataContext;
}
[OneTimeSetUp]
public void TestInitialise()
{
this.DataContext.Database.EnsureCreated();
}
[OneTimeTearDown]
public void TestTearDown()
{
this.DataContext.Database.EnsureDeleted();
}
}
上面給出了以下錯誤:
TestBase
沒有默認的構造函數。
我可能會以錯誤的方式進行操作,但這是我過去一直做的事情,因此,請讓我知道使用.NET Core DI時是否有更好的方法。
Startup
類供參考:
public class Startup
{
private readonly IConfiguration config;
public Startup(IConfiguration config)
{
this.config = config;
}
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<TestDataContext>(
options => options.UseSqlServer(this.config.GetConnectionString("TestConnectionString")),
ServiceLifetime.Singleton);
services.AddScoped<IEFDatabaseContext>(provider => provider.GetService<TestDataContext>());
}
}
感謝NightOwl為我指出正確的方向。 結合有關集成測試的Microsoft 文章和可能存在的重復問題,我找到了以下解決方案。
通過使用Microsoft.AspNetCore.TestHost
的TestServer
,我可以訪問Startup
內置的DI ServiceProvider
。
測試庫:
public partial class TestBase
{
protected readonly TestServer server;
protected readonly IEFDatabaseContext DataContext;
public TestBase()
{
this.server = new TestServer(new WebHostBuilder().UseStartup<Startup>());
this.DataContext = this.server.Host.Services.GetService<IEFDatabaseContext>();
}
[OneTimeSetUp]
public void TestInitialise()
{
this.DataContext.Database.EnsureCreated();
}
[OneTimeTearDown]
public void TestTearDown()
{
this.DataContext.Database.EnsureDeleted();
}
}
啟動:
public class Startup
{
private readonly IConfiguration config;
public Startup(IConfiguration config)
{
this.config = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.Build();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
}
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<TestDataContext>(
options => options.UseSqlServer(this.config.GetConnectionString("TestConnectionString")),
ServiceLifetime.Singleton);
services.AddScoped<IEFDatabaseContext>(provider => provider.GetService<TestDataContext>());
}
}
集成測試ASP.NET Core已在Microsoft文檔中很好地介紹了。
基本上,您需要從NuGet Microsoft.AspNetCore.TestHost
安裝Test Host項目,然后使用它在NUnit中啟動Web環境。
public class TestClass
{
private TestServer _server;
private HttpClient _client;
[OneTimeSetUp]
public void SetUp()
{
// Arrange
_server = new TestServer(new WebHostBuilder()
.UseStartup<Startup>());
_client = _server.CreateClient();
}
[OneTimeTearDown]
public void TearDown()
{
_server = null;
_client = null;
}
[Test]
public async Task ReturnHelloWorld()
{
// Act
var response = await _client.GetAsync("/");
response.EnsureSuccessStatusCode();
var responseString = await response.Content.ReadAsStringAsync();
// Assert
Assert.Equal("Hello World!",
responseString);
}
}
使用TestServer
可以干預DI配置和/或IConfiguration
來替代配置中的偽造品。 在集成測試ASP.NET Core Web API和EF Core時,請參閱重新配置依賴項 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.