Why does MSDTC behave inconsistently during unit testing with mstest?

I am having a strange problem when testing Nhibernate repositories.

I have 10 unit tests as shown below. Each time you run them in a package, the first failure is completed, and the rest is successful. If you run them one by one, they all fail. If restarting MSDTC before my test, it sometimes behaves as before, and sometimes all tests complete successfully. I can’t find the pattern why it behaves this way.

I want the transaction rollback to start with a clean DB for each test, hence deleting the transaction.

Test / tests do not work due to this error:

System.Data.SqlClient.SqlException: MSDTC on server "MYCOMPUTERNAME \ SQLEXPRESS" is not available.

My tests look like this:

[TestInitialize] public void MyTestInitialize() { _transactionScope = new TransactionScope(); } [TestCleanup] public void MyTestCleanup() { if (_transactionScope != null) { _transactionScope.Dispose(); _transactionScope = null; } } [TestMethod] [TestCategory("RepositoryTests")] public void RepositoryCanSaveAProduct() { var platform = ProductObjectMother.CreatePlatform("100010", "Supplier 10"); var mainsegment = ProductObjectMother.CreateMainSegment("123"); var application = ProductObjectMother.CreateApplication("Foo"); var productfamily = ProductObjectMother.CreateProductFamily("X99"); Engine i = ProductObjectMother.CreateEngine(platform, productfamily, application, mainsegment); var repository = new ProductRepository(); repository.Save(i); repository.Flush(); } 
+4
source share
1 answer

The problem seems to be related to a transaction that is not being passed using _transactionScope.Complete () or rollback by throwing an exception.

I also notice one strange thing, the test usually fails or is successfully executed using the "Assert" functions (equal, not equal, exists, etc. from assert) that are not in your test. :)

+2
source

All Articles