Greg's profileGreg Olsen - Yellow Duck...PhotosBlogLists Tools Help

Blog


    December 09

    System.TransactionScope with Unit Testing for both NUnit & Visual Studio Unit Testing

    One good thing about using System.Transactions with a unit test is the ability to perform a database unit test which then performs a rollback once the test is completed.

    (1) To create a unit test for NUnit with database rollback, simply add a 'using' statement similar to the example below.

    Note: you need to add a reference to your project to the System.Transactions namespace. Then add 'using System.Transactions;' to your class (.cs) file. You will also need the NUnit.Framework dll added as a project reference and also 'using NUnit.Framework;' to your class (.cs)

       1:  [Test]
       2:          public void BlogInsert()
       3:          {           
       4:              using (TransactionScope scope = new TransactionScope()) 
       5:              {
       6:                  DateTime postedDate = new DateTime(2006, 11, 23);
       7:                  string title = "XML Notepad 2006";
       8:                  string contents = "this is the contents from the blog";
       9:                  string blogUrl = "http://yellowduckguy.spaces.live.com/";
      10:   
      11:                  int expectedRowsAffected = 1;
      12:                  int actualRowsAffected = BlogDataAccess.BlogAdapterInsert(postedDate, title, contents, blogUrl);
      13:   
      14:                  Assert.AreEqual(expectedRowsAffected, actualRowsAffected);
      15:              }
      16:          }
     

    The key lines in the above code is line 4. This is where we add the 'using (TransactionScope ....' statement in order to wrap the statement into a transaction and allow the code to rollback against the database (I have been using this against SQL Server Database).

    (2) To create a unit test for Visual Studio with database rollback, simply change the the unit test attribute from [Test] to [TestMethod] and make sure you have reference to 'using Microsoft.VisualStudio.TestTools.UnitTesting;'

       1:          [TestMethod]
       2:          public void BlogInsert()
       3:          {           
       4:              using (TransactionScope scope = new TransactionScope()) 
       5:              {
       6:                  DateTime postedDate = new DateTime(2006, 11, 23);
       7:                  string title = "XML Notepad 2006";
       8:                  string contents = "this is the contents from the blog";
       9:                  string blogUrl = "http://yellowduckguy.spaces.live.com/";
      10:   
      11:                  int expectedRowsAffected = 1;
      12:                  int actualRowsAffected = BlogDataAccess.BlogAdapterInsert(postedDate, title, contents, blogUrl);
      13:   
      14:                  Assert.AreEqual(expectedRowsAffected, actualRowsAffected);
      15:              }
      16:          }

    Comments

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.

    To add a comment, sign in with your Windows Live ID (if you use Hotmail, Messenger, or Xbox LIVE, you have a Windows Live ID). Sign in


    Don't have a Windows Live ID? Sign up

    Trackbacks (1)

    The trackback URL for this entry is:
    http://yellowduckguy.spaces.live.com/blog/cns!DA380C13569E8907!128.trak
    Weblogs that reference this entry