問題描述
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_001、Duncan、David M、Roman Motyka)