Asserting That a Transaction was Completed

An entry about tdd Publication date 21. May 2008 13:33

I faced an interesting problem today when writing some code that was dealing with transactions. Basically, I had a method that created a TransactionScope and did some stuff before completing it. I wanted to write a test to verify that the transaction was not being aborted.

The solution was quite simple, actually. By creating my own transaction scope in the test, the one being created by the method I was testing would then become a nested transaction, part of the one I could control. This allowed me to do a simple assert on the the transaction status after calling the method:

[TestMethod]
public void Completes_the_transaction()
{
    // By creating a scope here, we make the one AssignSubstitute creates nested within the one we control,
    // which allows us to verify that it was committed
    using (new TransactionScope())
    {
        _assigner.AssignSubstitute(_substituteId, _unassignedItemId); // this method creates a TransactionScope
        Assert.AreEqual(TransactionStatus.Active,Transaction.Current.TransactionInformation.Status);
    }
}

 

By asserting that the transaction is still active after the nested scope has been polled, I can deduce that Complete was called in it - anything else will cause the Status to change (for example to Aborted).

 

kick it on DotNetKicks.com

Be the first to rate this post

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Comments

Powered by BlogEngine.NET 1.4.5.0

Welcome!

My name is Fredrik Kalseth, and this is my blog - thanks for visiting! I am fortunate enough to work with what I love for a living, and this blog is essentially the biproduct of that.

I work as a senior consultant for Capgemini, and am also an active participant in the Norwegian .NET community, as an avid attendee but also as a speaker (most recently at NNUG and MSDN Live).

As a developer, I have a wide circle of interest. My primary passion is for agile, test-driven development, with focus on best practices and clean code. That said, I also love to work on the frontend, especially with web development.

On Twitter? My handle is fkalseth. On LinkedIn? I`m there too.


Disclaimer

This is a personal blog; any opinions expressed here are my own and do not necessarily reflect those of my employer. All content herein is my own original creation, and as such is protected by copyright law. Unless otherwise stated, all source code posted on this blog is freely usable under the Microsoft Permissive License.

What Readers Talk About

Comment RSS