Tuesday, January 13, 2009

Reset Database after Unit Tests are run

I was doing RnD using VSTS 2008 for Unit tests. One of the issue I faced was the data used to get changed after my tests were passed. This scenario created one more need for me to find a method to rollback what ever changes my Unit tests used to make to my database.

I tried out MdbUnit, NUnit but I was still searching for something which is provided by Microsoft itself. Reading to few blogs I successfully made it work.

[TestInitialize]
public void Initialize()
{
ServiceConfig config = new ServiceConfig();
config.Transaction = TransactionOption.RequiresNew;
ServiceDomain.Enter(config);
}

[TestCleanup]
public void Cleanup()
{
if (ContextUtil.IsInTransaction)
{
ContextUtil.SetAbort();
}
ServiceDomain.Leave();
}

After I wrote this in my Test.cs file I faced another error
system.Data.Entity Exception: The underlying provider failed on Open. ---> System.Transactions.TransactionManagerCommunicationException: Network access for Distributed Transaction Manager (MSDTC) has been disabled.

The following link helped me

http://social.msdn.microsoft.com/Forums/en-US/windowstransactionsprogramming/thread/993b1b87-b9c0-4070-938b-8539fcb059f6/

Wallah my Unit test problem is solved!

No comments: