繁体   English   中英

单元测试方法的IO操作的自定义回滚属性

[英]Custom rollback attribute for IO operations for unit test methods

我具有测试方法的“回滚”属性,用于测试使用数据库事务的方法:

  public class RollbackAttribute : Attribute, ITestAction
  {
      private TransactionScope transaction;
      public void BeforeTest(TestDetails testDetails)
      {
          transaction = new TransactionScope();
      }

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

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

有什么方法可以为使用IO(创建文件夹)操作的方法创建类似的属性?

在C#中,文件I / O实现未使用事务,因此您无法回滚它们。 您能做的最好的就是为此编写自己的机制。

我不会复制其他文章,但是在这里您有一个有趣的示例,说明如何实现自己的事务存储库。

我发现带有实现的nuget包更有趣: LINK

这里有用法示例:

// Wrap a file copy and a database insert in the same transaction
TxFileManager fileMgr = new TxFileManager();
using (TransactionScope scope1 = new TransactionScope())
{
    // Copy a file
    fileMgr.Copy(srcFileName, destFileName);

    // Insert a database record
    dbMgr.ExecuteNonQuery(insertSql);

    scope1.Complete();
} 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM