如何将 DbContext 添加到 MSTest 项目?
How do I add a DbContext to an MSTest project?
我试图测试一些使用 Entity Framework 的代码,但我不知道如何从单独的 MSTest 项目中引用 EF 上下文 类。这两个项目都在同一个解决方案中。
Cannot convert lambda expression to type 'DbContextOptions' because it is not a delegate type
在我的测试用例中:
[TestClass]
public class GreenCardUserTest
{
[TestMethod]
public void TestAddUser()
{
// REFERENCE TO OTHER PROJECT. WORKS FINE
AppUserViewModel a = new AppUserViewModel();
//LIKELY INCORRECT attempt to duplicate code from Startup.cs in other project
using (GreenCardContext _gc = new GreenCardContext(options => options.UseSqlServer(Configuration.GetConnectionString("MyConnection"))))
{
new GCLandingUserModel().AddUser(a,_gc);
}
}
}
主项目摘录Startup.cs(效果很好):
services.AddDbContext<GreenCardContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("MyConnection")));
您从 Startup.cs
获得的代码使用委托告诉您的应用程序如何在运行时构建 DbContext。
但是在您的测试中,您实际上需要提供 DbContextOptions
的一个实例,而不仅仅是一个委托。为此,您可以使用 DbContextOptionsBuilder
:
var options = new DbContextOptionsBuilder<GreenCardContext>()
.UseSqlServer(Configuration.GetConnectionString("MyConnection"))
.Options;
using (GreenCardContext _gc = new GreenCardContext(options))
{
new GCLandingUserModel().AddUser(a,_gc);
}
此外,如果您坚持对 DbConext 进行单元测试,您可能需要考虑使用 InMemoryDatabase,这样您就不需要在测试中打开 SQL 连接。有关详细信息,请参阅 this document。
您需要做的是:
1) 在您的测试项目中添加对上下文项目的引用(如果您还没有的话)
2) 将对 Entity Framework 的引用添加到您的测试项目
3) 将 appconfig 添加到您的测试项目并在其上设置 entity framework 配置。你的测试将从它自己的配置中读取配置,而不是你的应用程序。非常有用,例如,在测试中使用 dblocal 和 codefirst,在 运行 上使用 sqlserver :)
你已经完成了一些,我认为你缺少的是第三点:)
我建议使用 InMemoryDatabase:
在您的测试 class 中,使用 [TestInitialize] 设置您的虚拟数据库:
[TestClass]
public class GreenCardUserTest
{
private readonly context;
[TestInitialize]
public Setup()
{
DbContextOptions<GreenCardContext> options;
var builder = new DbContextOptionsBuilder<GreenCardContext>();
builder.UseInMemoryDatabase();
var options = builder.Options;
context = new GreenCardContext(options);
}
[TestMethod]
public void TestAddUser()
{
// user context here...
}
}
我试图测试一些使用 Entity Framework 的代码,但我不知道如何从单独的 MSTest 项目中引用 EF 上下文 类。这两个项目都在同一个解决方案中。
Cannot convert lambda expression to type 'DbContextOptions' because it is not a delegate type
在我的测试用例中:
[TestClass]
public class GreenCardUserTest
{
[TestMethod]
public void TestAddUser()
{
// REFERENCE TO OTHER PROJECT. WORKS FINE
AppUserViewModel a = new AppUserViewModel();
//LIKELY INCORRECT attempt to duplicate code from Startup.cs in other project
using (GreenCardContext _gc = new GreenCardContext(options => options.UseSqlServer(Configuration.GetConnectionString("MyConnection"))))
{
new GCLandingUserModel().AddUser(a,_gc);
}
}
}
主项目摘录Startup.cs(效果很好):
services.AddDbContext<GreenCardContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("MyConnection")));
您从 Startup.cs
获得的代码使用委托告诉您的应用程序如何在运行时构建 DbContext。
但是在您的测试中,您实际上需要提供 DbContextOptions
的一个实例,而不仅仅是一个委托。为此,您可以使用 DbContextOptionsBuilder
:
var options = new DbContextOptionsBuilder<GreenCardContext>()
.UseSqlServer(Configuration.GetConnectionString("MyConnection"))
.Options;
using (GreenCardContext _gc = new GreenCardContext(options))
{
new GCLandingUserModel().AddUser(a,_gc);
}
此外,如果您坚持对 DbConext 进行单元测试,您可能需要考虑使用 InMemoryDatabase,这样您就不需要在测试中打开 SQL 连接。有关详细信息,请参阅 this document。
您需要做的是:
1) 在您的测试项目中添加对上下文项目的引用(如果您还没有的话)
2) 将对 Entity Framework 的引用添加到您的测试项目
3) 将 appconfig 添加到您的测试项目并在其上设置 entity framework 配置。你的测试将从它自己的配置中读取配置,而不是你的应用程序。非常有用,例如,在测试中使用 dblocal 和 codefirst,在 运行 上使用 sqlserver :)
你已经完成了一些,我认为你缺少的是第三点:)
我建议使用 InMemoryDatabase:
在您的测试 class 中,使用 [TestInitialize] 设置您的虚拟数据库:
[TestClass]
public class GreenCardUserTest
{
private readonly context;
[TestInitialize]
public Setup()
{
DbContextOptions<GreenCardContext> options;
var builder = new DbContextOptionsBuilder<GreenCardContext>();
builder.UseInMemoryDatabase();
var options = builder.Options;
context = new GreenCardContext(options);
}
[TestMethod]
public void TestAddUser()
{
// user context here...
}
}