我应该如何设置我的集成测试使用实体框架测试数据库? [英] How should I set up my integration tests to use a test database with Entity Framework?
问题描述
我写集成测试的一个应用程序,并没有能够找到如何建立一个测试数据库为我的集成套件的最佳做法。我正在使用实体框架code-第一个ASP.NET应用程序MVC4
I am writing integration tests for an application and have not been able to find any best practices on how to set up a test database for my integration suite. I am working on an ASP.NET MVC4 application using Entity Framework code-first.
我可以证实,在我的测试项目中聊到本地开发数据库在我的机器上默认的测试。这是不理想的,因为我想我每次运行测试时有一个全新的数据库。
I can confirm that the tests in my test project talk to the local development database on my machine by default. This is not ideal, as I want to have a fresh database every time I run the tests.
如何设置我的测试项目,使我的测试跟一个单独的实例?我假设,有可能建立一个SQL Server精简版的实例,但我不知道如何配置这一点。
How can I set up my test project so that my tests talk to a separate instance? I'm assuming that it is possible to set up an SQL Server Compact Edition instance, but I'm not sure how to configure this.
推荐答案
感谢这么多@Justin和@Petro你的答案,这些都极大地帮助了我。我想出了解决的办法是你提出的技术的组合。下面描述的解决方案提供了一个新的数据库为测试的每个运行,并为每个测试单独的事务
Thanks so much to @Justin and @Petro for your answers, which have helped me immensely. The solution I have come up with is a combination of the techniques you suggested. The solution described below provides a new database for each run of the tests, and a separate transaction for each test.
我添加了一个连接字符串在我的测试项目的App.config中我的测试数据库:
I added a connection string for my test database in the App.config of my Test project:
<connectionStrings>
<add name ="TestDatabase"
providerName="System.Data.SqlClient"
connectionString="Data Source=(LocalDb)\v11.0;Database=TestDatabase;Integrated Security=True"/>
</connectionStrings>
我创建了一个基类为我的集成测试,为客户提供安装和拆卸。设置实例的上下文中,创建DB如果不存在,并开始一个事务它。拆解回滚事务。
I created a base class for my integration tests, to provide setup and teardown. Setup instantiates the context, creates the DB if it doesn't exist yet and starts a transaction. Teardown rolls back the transaction.
public class EntityFrameworkIntegrationTest
{
protected MyDbContext DbContext;
protected TransactionScope TransactionScope;
[TestInitialize]
public void TestSetup()
{
DbContext = new MyDbContext(TestInit.TestDatabaseName);
DbContext.Database.CreateIfNotExists();
TransactionScope = new TransactionScope(TransactionScopeOption.RequiresNew);
}
[TestCleanup]
public void TestCleanup()
{
TransactionScope.Dispose();
}
}
最后,我有一个类,它删除数据库之后,所有的测试都运行的护理:
Finally, I have a class that takes care of deleting the database after all the tests have run:
[TestClass]
public static class TestInit
{
// Maps to connection string in App.config
public const string TestDatabaseName = "TestDatabase";
[AssemblyCleanup]
public static void AssemblyCleanup()
{
Database.Delete(TestDatabaseName);
}
}
我要补充一点,我发现<一个href=\"http://blogs.msdn.com/b/adonet/archive/2010/09/02/ef-feature-ctp4-dbcontext-and-databases.aspx\">this博客文章的实体框架有用的东西实体框架的引擎盖下/由做一个约定更深入的了解。
I should add that I found this blog post about Entity Framework useful for a deeper understanding of what Entity Framework is doing under the hood / by convention.
这篇关于我应该如何设置我的集成测试使用实体框架测试数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!