进行回滚-存储库集成测试 [英] Doing a rollback - Repository integration tests

查看:86
本文介绍了进行回滚-存储库集成测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对我的实体框架驱动的存储库进行集成测试。问题是测试完成后如何回滚数据库状态。目前,我正计划在测试SetUp上开始交易,并在测试TearDown上回滚。除了手动清除数据库外,还有其他解决方案吗?

I want to implement integration tests of my Entity Framework driven repositories. The problem is how to rollback database state after tests are done. At the moment I'm planning to start transaction at test SetUp and roll it back at test TearDown. Are there any other solutions excepting manual database clearing?

推荐答案

我们在使用MSTest的集成测试中执行此操作。我们使用 TransactionScope 并在基类中实现测试设置和拆卸。这使您可以在事务中运行所有集成测试。基类看起来很像这样:

We do this in our integration tests while using MSTest. We use the TransactionScope and implement a test setup and teardown in a base class. This allows you to run all integration tests within a transaction. The base class looks much like this:

public class IntegrationTestsBase
{
    private TransactionScope scope;

    [TestInitialize]
    public void Initialize()
    {
        this.scope = new TransactionScope();
    }

    [TestCleanup]
    public void TestCleanup()
    {
        this.scope.Dispose();
    }
}

祝你好运。

这篇关于进行回滚-存储库集成测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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