TransactionalUnitTestBase.cs

Reusable Class

Name

Description

Last Modified Date

Download

 TransactionalUnitTestBase.cs

 Use as a base class in your Visual Studio based Unit Test projects. 

 6/Oct/2014

None

TransactionalUnitTestBase.cs
#region
 Usings

using System;
using System.Collections;
using System.Transactions;
using Microsoft.VisualStudio.TestTools.UnitTesting;

#endregion
namespace UnitTest
{
/// <!-- 
///     Code Courtesy Of https://codemonkeysoftware.atlassian.net
///     Permissions : Free for general use
///     Keywords: Diagnostics, Unit Testing, c#, .net, Windows, aspnet. 
///     Filename: TransactionalUnitTestBase.cs
/// -->
/// <summary>
///     This class provides Transactional ability to unit tests.  Similar to the com+ approach, all code
///     executed within the scope of this class will rollback because no commit is called.
///     The TearDown function will Dispose the transaction to avoid locking issues with other tests.
///     If you have a Transaction import issue while debugging, increase the timeout.
/// </summary>
/// <remarks>
///     This class will only work with .Net 2.0 and above due to the use of <see cref="TransactionScope" />
///     Last updated: 6/10/2014
/// </remarks>
     
public abstract class TransactionalUnitTestBase : UnitTestBase
{
protected Hashtable _ForeignEntities = new Hashtable();
private TransactionScope _Transaction;
private TimeSpan _TransactionTimeout = new TimeSpan(0, 0, 10, 0);
public TransactionalUnitTestBase()
{
_Transaction = new TransactionScope(TransactionScopeOption.RequiresNew,
                _TransactionTimeout);
            
//Stops any database commits - Requires reference to System.Transactions - .Net 2+
         }
[TestCleanup]
public void Rollback()
{
_Transaction
.Dispose();
            
//Setup new transaction
_Transaction = new TransactionScope(TransactionScopeOption.RequiresNew,
                _TransactionTimeout);
}
[ClassCleanup]
public virtual void UnitTestTearDown()
{
	_Transaction.Dispose();
}
    }
}

CodeMonkey Software is a division of JCHMedia www.jchmedia.com