Rollin’ rollin’ rollin’ – rollin’ back your data back in integration tests

Just got back from Iowa Code Camp 7, which was fantastic btw.
Had a funny moment sitting in Lee Brandt’s nHibernate demo where two of my co-workers and I all realized that we were doing some rocket surgery. We had just started refactoring a legacy codebase by adding integration tests and were manually rolling back the database changes with several lines of handwritten sql deletes… (sorry). Looking at Lee’s demo code, we saw him using a transaction for the rollbacks and I hung my head in shame.

Thank you Mr. Tim Squires for writing out this code sample for me:

using NUnit.Framework;
using System.Transactions;
namespace ProjectName.Tests.Integration
{
    [TestFixture]
    public class TestClass
    {
        private TransactionScope _transactionScope;
        [SetUp]
        public void SetUp()
        {
            _transactionScope = new TransactionScope(TransactionScopeOption.RequiresNew);
        }
        //...Test some stuff, insert records, update records, etc...
        [TearDown]
        public void TearDown()
        {
            _transactionScope.Dispose();
        }
    }
}

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.