Skip to content

Instantly share code, notes, and snippets.

@jeffjohnson9046
Last active February 17, 2018 20:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeffjohnson9046/2e0562ecffa26c1844f473bbee809053 to your computer and use it in GitHub Desktop.
Save jeffjohnson9046/2e0562ecffa26c1844f473bbee809053 to your computer and use it in GitHub Desktop.
A C# class for rolling back database modifications after they've happened.
/// <summary>
/// A class for rolling back database transactions after the test has completed.
/// </summary>
[TestClass]
public abstract class TransactionRollbackIntegrationTestBase
{
private TransactionScope transactionScope;
/// <summary>
/// Create the transaction scope to enforce creating a new transaction prior to the test executing.
/// </summary>
[TestInitialize]
public virtual void Setup()
{
transactionScope = new TransactionScope(TransactionScopeOption.RequiresNew, new TransactionOptions { Timeout = new TimeSpan(0, 10, 0) });
}
/// <summary>
/// Rollback the transaction and cleanup the transaction scope after test execution completes.
/// </summary>
[TestCleanup]
public virtual void Teardown()
{
Transaction.Current.Rollback();
transactionScope.Dispose();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment