tags:

views:

363

answers:

4

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 :(

A: 

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).

David M
I am aware of the transaction scope, but I want to know how to do this with the MbUnit framework. Is this possible?
Pippen_001
Yes. The Rollback attribute is not the preferred approach - the Rollback2 attribute I believe is the preferred solution for .NET 2.0 and above.
David M
A: 

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.

bezieur
A: 

How are you running the test? I mean using what runner?

Igor Brejc
I am using the TestDriven.NET tool. Wheither it be via debugging or ncover.. I just want to know how to get this running.
Pippen_001
The reason I'm asking is because the problem could be an outdated TD.NET version. I suggest you try the test using a command line MbUnit runner.
Igor Brejc
+1  A: 

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