MbUnit 回滾 (MbUnit Rollback)


問題描述

MbUnit 回滾 (MbUnit Rollback)

I'm testing the MbUnit Framework and want to keep my test database in a persistent state after each test. How can I accomplish this? 

This is what I'm trying, but my table is filled after the test is completed.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Gallio.Framework;
using MbUnit.Framework;

using NHibernate;
using NHibernate.Cfg;

namespace BusinessLayer.Tests
{
    [TestFixture]
    public class PersonNHibernateTests
    {
        [Test] 
        [Rollback]
        public void CanSavePerson()
        {
            Configuration config = new Configuration();
            config.Configure();
            ISessionFactory factory = config.BuildSessionFactory();

            using (ISession session = factory.OpenSession())
            {
                using (ITransaction tx = session.BeginTransaction())
                {

                    const string CONST_STR_FIRSTNAME = "Stephen";
                    const string CONST_STR_LASTNAME = "Manga";
                    DateTime birthdate = new DateTime(1974, 6, 20);

                    Person p = new Person
                    {
                        FirstName = CONST_STR_FIRSTNAME,
                        LastName = CONST_STR_LASTNAME,
                        Birthdate = birthdate
                    };

                    session.SaveOrUpdate(p);
                    session.Flush();

                    tx.Commit();

                }

            }
        }

    }
}

Edit:

After some reading I've come to the understanding that Distributed Transaction Coordinator has to be enabled. After starting this service and testing still no success :(

‑‑‑‑‑

參考解法

方法 1:

You have a COMMIT statement in your code.  Perhaps you should remove that.

方法 2:

Why not just let System.Transactions.TransactionScope handle it?

using (new TransactionScope())
{
    // do stuff that gets automatically rolled back
}

Alternatively, this seems to be exactly what the MbUnit Rollback2 attribute does anyway (Rollback uses EnterpriseServices/COM+ and is aimed at .NET 1.1).

方法 3:

I use Proteus it does just fine. Easy to setup and use..  All you need is to add some code to Setups TearDowns and prepare folder with 'snapshot' of your database.

(by Pippen_001DuncanDavid MRoman Motyka)

參考文件

  1. MbUnit Rollback (CC BY‑SA 3.0/4.0)

#mbunit #tdd #C#






相關問題

我可以圍繞 NUnit、MbUnit、xUnit 或其他測試框架創建一個包裝器嗎? (Can I create a wrapper around NUnit, MbUnit, xUnit or other testing framework?)

MbUnit / Gallio 上的 FixtureSetup 未運行 (FixtureSetup on MbUnit / Gallio doesn't run)

我可以使用我的 WatiN 測試來進行壓力測試嗎? (Can i use my WatiN tests to stresstest?)

Debug Unit Test bằng cách sử dụng Resharper 7 cho MBUnit ném ra một ngoại lệ (Debug Unit Tests using Resharper 7 for MBUnit throws an exception)

TestDriven.NET沒有運行我的MbUnit的SetUp方法 (TestDriven.NET is not running my SetUp methods for MbUnit)

Gallio用戶,您使用此工具有哪些優缺點? (Users of Gallio, what Advantages and Disadvantages have you experienced using this Tool?)

你知道任何關於 MBUnit 的教程嗎? (do you know any tutorial for MBUnit?)

MbUnit:比較雙打最優雅的方式? (MbUnit: The most elegant way to compare doubles?)

如何將 MBUnit 測試添加到 CruiseControl.Net 配置文件 (how to add MBUnit test to CruiseControl.Net config file)

MbUnit 回滾 (MbUnit Rollback)

使用 MbUnit3 的 [Rollback] 對 NHibernate 與 SQLite 的交互進行單元測試 (Using MbUnit3's [Rollback] for unit testing NHibernate interactions with SQLite)

MbUnit.framework.dll 可並行化屬性 (MbUnit.framework.dll Parallelizable attribute)







留言討論