views:

462

answers:

2

We have some unit tests running against a SQL server 2000 database using the DatabaseTestFixture (http://softwaredevscott.spaces.live.com/blog/cns!1A9E939F7373F3B7!155.entry ) class which uses a TransactionScope that is not commited and therefore all changes are rolled back. The tests ran against a local database with no problem.

We then pointed the unit tests at a common database server and setup MSDTC on the machine running the unit tests and all is well with that machine.

Now though we have setup a new machine which will be running the unit test also against the same common database. The unit test do not run on this machine, we get the following error:

System.Transactions.TransactionException : The transaction has already been implicitly or explicitly committed or aborted.

The MSDTC settings are exactly the same as the one that is working, we have compared screen by screen.

Has anyone experienced something like this before? Or got any pointers on where we might look for clues as to what might be causing it?

We have checked Windows versions and service packs, firewall options, msdtc options, VS versions and service packs.

A: 

Hi JRyan

this problem is probably related to the version of Nunit you're using. I had exactly the same issue when running unit 2.5 but the problem mysteriously disappears when I run the unit tests with nunit 2.4.8 or 2.2.

Please try this any let me know so I boost my puny profile points!

Regards

MAC

I'm currently using nunit 2.2.8 though - I may try a reinstall to see if that helps. If it does I'll give you the points! :-)
jryan
I've just checked, and this is indeed the situation.I've got several tests that work with 2.4.8-net-2.0, but when moved to 2.5.0-net-2.0 fail with transaction has aborted.
Bruno Lopes
+1  A: 

This seems to be related to NUnit 2.5. Before it, the following code worked fine:

[SetUp]
public void SetUp() {
    this._testDataContext = new DataContext();
    this._transactionScope = new TransactionScope();
}

[TearDown]
public void TearDown() {
    if (_transactionScope != null)
    {
        this._transactionScope.Dispose();
    }
    if (_testDataContext != null)
    {
        _testDataContext.Dispose();
    }
}

With 2.5 the transaction scope needs to instanciated with TransactionScopeOption.RequiresNew. I assume that either it isn't being disposed correctly, or that NUnit has some ambient transaction running when the tests are being run. Change the second line on the SetUp method to

this._transactionScope = new TransactionScope(TransactionScopeOption.RequiresNew);

and it should work correctly.

If someone manages to get a better explanation as to why this happens, I'd love to hear it.

Bruno Lopes