I would like to run some tests of stored procedures in my database, without actually affecting the data (or, more precisely, without prolonged exposure after running the test).
After some research, I came up with an approach to using TransactionScope in my Visual Studio 2010 test project, for example
using( new TransactionScope()) { using( SqlConnection connection = new SqlConnection("someConnectionString")) { connection.Open(); using( SqlCommand command = new SqlCommand( "some sql", connection )) {
Now it works great, as long as I put all this into one test method, i.e. all my changes to the database are automatically rolled back when using the TransactionScope block.
My problem is that I would like to do some database stuff in ClassInitialize, so I need to do this only once for each test class, and not for each test method. When I create a public TransactionScope property and assign it a TransactionScope instance in the ClassInitialize method, this works fine. As soon as I make any database related material in one of my test methods, I throw a TransactionManagerCommunicationException inside this method.
I donβt quite understand why this is so, and I would also like to know if there is an error in my approach or how I can make it work without the need to configure TransactionScope, including all the configured material for tests within each test method again.
EDIT
An excerpt from the code below, I hope this gives enough information:
public TransactionScope Scope { get; set; } [ClassInitialize] public static void ClassInitialize( TestContext testContext ) { Scope = new TransactionScope();
The exception is
TransactionManagerCommunicationException was not handled by user code Network access for Distributed Transaction Manager (MSDTC) has been disabled. Enable DTC to access the network in the security configuration for MSDTC using the component services administration tool.
I understand that I can try and change the settings, but I donβt understand why I am throwing an exception - how is it different from the code above in one (test) method?
Thanks in advance and
Best wishes
G.