重新创建和重新设定种子的LocalDB每个单元测试之前 [英] Recreate and Reseed LocalDb Before Each Unit Test

查看:104
本文介绍了重新创建和重新设定种子的LocalDB每个单元测试之前的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图编写单元/集成测试我的ASP.NET Web API项目,并奋力奔跑在隔离每个测试。请允许我解释一下。

I'm attempting to write unit/integration tests for my ASP.NET Web API project and struggling to run each test in isolation. Allow me to explain.

我有一个* .testsettings与配置部署设置文件。每次试验前,一个空的* .mdf文件部署到测试位置。由于我使用的实体框架code首先,我可以使用数据库初始化到我的模式推到数据库和种子特定表2行。这个伟大的工程。

I have a *.testsettings file with deployment settings configured. Before each test run, an empty *.mdf file is deployed to the test location. Since I'm using Entity Framework Code First, I can use a database initializer to push my schema to the database and seed a particular table with 2 rows. This works great.

我现在面临的问题是,我所有的ApiControllers行动的各种测试踩到对方的脚,如果他们在错误的顺序执行。例如,如果我的POST测试之前运行GET测试然后get返回两个对象,如果他们在相反的顺序,然后get返回3个对象运行。

The problem I'm facing is that the various tests for all of my ApiControllers' actions can step on each other's toes if they execute in the wrong order. For example, if I run the GET test before the POST test then GET returns 2 objects with if they are run in the reverse order then GET returns 3 objects.

我想我需要做的是下降,并重新创建每次测试之前补种我的数据库。这是一个好主意,或者有更好的办法?如果这是我能做到的最好,我想我去每个测试之前重置我的数据库。

What I think I need to do is drop, recreate and reseed my database before every test. Is this a good idea or is there are better way? If this is the best I can do, I would I go about resetting my database before each test.

推荐答案

下面是我落得这样做的人是有兴趣的。

Here's what I ended up doing for anyone that is interested.

我延长 DropCreateDatabaseAlways< TContext> 和推翻种子方法来填充我的用已知的测试数据的数据库,我的单元测试可以依靠

I extended DropCreateDatabaseAlways<TContext> and overrode the Seed method to populate my database with known test data that my unit tests can rely on.

public class MyDatabaseInitializer : System.Data.Entity.DropCreateDatabaseAlways<MyDbContext>
{
    protected override void Seed(MyDbContext context)
    {
        // Add entities to database.

        context.SaveChanges();
    }
}

然后我实施了将数据库初始化一个 [AssemblyInitialize] 方法。

[TestClass]
public class Global
{
    [AssemblyInitialize]
    public static void AssemblyInitialize(TestContext context)
    {
        System.Data.Entity.Database.SetInitializer(new MyDatabaseInitializer());
    }
}

这设置数据库初始化但不实际运行它。该intitializer是在 [TestInitialize] 方法我写这迫使数据库运行要删除,重新创建和重新接种每次测试之前。

This sets the initializer for the database but does not actually run it. The intitializer is run in a [TestInitialize] method I wrote which forces the database to be dropped, recreated and reseeded before each test.

[TestClass]
public class MyTestClass
{
    [TestInitialize]
    public void TestInitialize()
    {
        MyDbContext context = new MyDbContext();
        context.Database.Initialize(true);
    }

    [TestMethod]
    public void MyUnitTest()
    {
        Assert.IsTrue(true);
    }
}

这篇关于重新创建和重新设定种子的LocalDB每个单元测试之前的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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