NUnit 回滚属性似乎在 SQL Server 2005 上失败 [英] NUnit Rollback attribute seems to fail on SQL Server 2005

查看:51
本文介绍了NUnit 回滚属性似乎在 SQL Server 2005 上失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 NUnit 数据库级测试中使用了一个不错的 [Rollback] 属性:

I'm using a nice little [Rollback] attribute on my NUnit database-level tests:

public class RollbackAttribute : Attribute, ITestAction 
{
   private TransactionScope _transaction;

   public ActionTargets Targets {
      get { return ActionTargets.Test; }
   }

   public void BeforeTest(TestDetails testDetails) {
      _transaction = new TransactionScope();
   }

   public void AfterTest(TestDetails testDetails) {
      _transaction.Dispose();
   }
}

所以我可以像这样装饰我的基于数据库的测试:

So I can then decorate my db-based tests like this:

[Test]
[Rollback]
public void TestGetAllActiveItems()
{
    // Arrange
    SetupTestData();

    // Act
    var results = GetAllActiveItems(string.Empty, string.Empty);

    // Assert
    Assert.IsNotNull(results);
}

我在 SetupTestData() 方法中创建和存储的示例数据用于测试,然后在测试结束时丢弃.

The sample data I create and store in the SetupTestData() method is used for the test, and then discarded at the end of the test.

这在我本地开发机器上的 SQL Server 2012 和 2014 上很有效 - 但由于某种原因,它似乎在我们的构建和运行中失败了.测试机,仍在使用SQL Server 2005(即将升级).

This works like a charm on SQL Server 2012 and 2014 on my local dev machine - but for some reason, it appears to fail miserably on our build & test machine, which is still using SQL Server 2005 (soon to be upgraded).

有什么想法吗?我只是看到我在 SetupTestData() 方法中插入数据库的两个项目为每个测试方法插入一次,并且 NOT 在每个测试方法的末尾回滚方法,出于某种奇怪的原因......我在构建/测试运行日志中没有看到任何错误或其他指标 - 它只是不起作用并且不会对我的构建进行回滚/测试服务器.

Any ideas why? I just see that my two items that I insert into the database in my SetupTestData() method are inserted once for each test method, and NOT rolled back at the end of each method, for some weird reason.... I don't see any errors or other indicators in the build / testrun logs - it just doesn't work and doesn't do the rollback on my build/test server.

有什么指点吗?想法?要检查的要点?

Any pointers? Thoughts? Points to check?

推荐答案

基于 这篇文章 描述了 SQL2005 的细节我认为您的问题可能是您对所有事务重用相同的连接,引用:

Based on this article which describes specifics of SQL2005 I would presume your problem may be that you are reusing the same connection for all the transactions, quoting:

在 SQL Server 2005 中,使用 TransactionScope 启动本地事务,而不是分布式事务.无论是将 TransactionScope 与客户端代码还是 SQLCLR 过程一起使用,这都是行为,除非在调用 SQLCLR 过程时已经启动了一个事务.

In SQL Server 2005, using the TransactionScope starts a local, not a distributed, transaction. This is the behavior whether TransactionScope is used with client-side code or SQLCLR procedures unless there is already a transaction started when the SQLCLR procedure is invoked.

...

事务实际上是在 SqlConnection 上调用 Open 时开始的,而不是在创建 TransactionScope 实例时开始.

The transaction actually begins when Open is called on the SqlConnection, not when the TransactionScope instance is created.

...

此时,由于 SQL Server 2005 不支持同一连接上的自治事务,SqlConnection 的 BeginTransaction 方法是本地事务的最佳选择.

At this point, because SQL Server 2005 doesn’t support autonomous transactions on the same connection, SqlConnection’s BeginTransaction method is the best choice for local transactions.

请告诉我这条信息是否有效,因为我目前无法自己测试问题.

Please let me know whether this piece of info is valid as I currently cannot test the problem myself.

这篇关于NUnit 回滚属性似乎在 SQL Server 2005 上失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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