我应该如何设置我的集成测试以使用实体框架的测试数据库? [英] How should I set up my integration tests to use a test database with Entity Framework?

查看:104
本文介绍了我应该如何设置我的集成测试以使用实体框架的测试数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为应用程序编写集成测试,并且无法找到有关如何为我的集成套件设置测试数据库的最佳做法。我正在使用实体框架代码编写一个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 Compact Edition实例,但我不知道如何配置这个。

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.

我在应用程序中为我的测试数据库添加了一个连接字符串我的测试项目的.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>

我为我的集成测试创建了一个基类,提供设置和拆卸。安装程序实例化上下文,创建数据库(如果尚不存在)并启动事务。拆分回滚交易。

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);
    }
}

我应该补充说,我发现本博客关于实体框架有助于更深入地了解实体框架正在根据惯例进行的操作。

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆