jeudi 22 octobre 2015

Can I create an ambient transaction for testing?

My current system has a rather aggressive data layer that creates an SqlDatabase instance for me by calling a static method. I pass in a stored procedure name (string) and the magic just happens.

I want to try and get some of this crazy system under test and so want to control what is in the database.

Having realised that this structure

[Test]
public void Should_do_some_thing()
{   
  using (var scope = new TransactionScope())
            {
                CleanUpDatabase();
                SetupDatabaseData();

                //Run Test

                Assert.That(someResult,Is.EqualTo("ExpectedValue");

                scope.Dispose();
            }
}

does what I want (no database changes persist outside the test) It would clearly be nicer if I could set up the transaction within a [SetUp] method and remove without committing in the [TearDown] section.

Is this possible?

Note I cannot call any methods on a command object or whatever...

Aucun commentaire:

Enregistrer un commentaire