I use MSTest to run some automated tests using MySQL 5.5.19 DB via the MySQL Connector and using EntityFramework 4.3.
I am trying to use TransactionScope in my DB access class library to roll back if necessary. Also, in my test code, I want to use TransactionScope to return the database to a known state before each test. For this, I use the TestInitialize and TestCleanup . They look like this:
[TestInitialize()] public void MyTestInitialize() { testTransScope = new TransactionScope(TransactionScopeOption.RequiresNew); } [TestCleanup()] public void MyTestCleanup() { Transaction.Current.Rollback(); testTransScope.Dispose(); }
Based on building the TransactionScope object there in the initialization function, I believe that I should get a new transaction area (there is no existing "ambient", so I believe that this .RequiresNew isnβt technically important here, because ".Required" will be give the same result. Since I do not specify a timeout value, it provides me with a default timeout, which, as I understand it, is 60 seconds. A lot of time for my given test.
I have a function called AddDessert(DessertBiz dessertBizObject) , which in particular looks something like this:
using (var transScope = new TransactionScope(TransactionScopeOption.Required)) { try {
And this function is called by one of my tests.
Since I specified TransactionScopeOption.Required here, I expect it to use the "ambient" transaction scope created by the MyTestInitialize function.
My test is designed to make this DoOtherDessertStuff function unsuccessful and throw an exception, so calling transScope.Complete(); does not happen, and rollback occurs automatically when you exit the using block in the AddDessert function.
The problem I have here is that since it uses the volume of the external transaction created in the MyTestInitialize function, my Assert calls are not executed because the transaction scope was rolled back - at least that is what I think is happening. I have confirmed that Transaction.Current.TransactionInformation.Status TransactionStatus.Aborted , so I am sure that this is what is happening.
Great, so I decided to change my AddDesert method to look exactly the same as above, except that I would nest the transaction area rather than use ambient, some of my using lines look like this:
using (var transScope = new TransactionScope(TransactionScopeOption.RequiresNew))
The goal was that I could nest these transaction areas, roll back in my production code, and then still check my Assert in my test code.
But I find that I get the following error:
System.IO.IOException: cannot read data from the transport connection: the connection attempt failed because the connected party did not respond properly after some time or the connection was not established because the connected host could not respond.
Ideas?