views:

218

answers:

1

I am trying to do some automated web testing of my ASP.NET application. I was hoping to use the AutoRollback attribute from Xunit.net extensions to undo any database changes that were made during the test. AutoRollback uses TransactionScope to start a transaction before the test and roll it back afterwards.

When I try to hit my web application during a transaction, it always times out. It seems like this should work, any ideas? Here is my test:

[Fact]
[AutoRollback]
public void Entity_should_be_in_list()
{
    Entity e = new Entity
    {
        Name = "Test",
    };
    dataContext.Entities.InsertOnSubmit(e);
    dataContext.SubmitChanges();

    selenium.Open("http://localhost/MyApp");
    Assert.True(selenium.IsTextPresent("Test"));
}
+2  A: 

Your ASP.NET application has a separate database context and it has no idea that you want it to join transaction started by Xunit.net. Apparently, the database locks some resources when transaction starts; web application waits patiently for some time and eventually gives up.

I think your best bet is to start from empty database and use SQL script to create schema and populate lookup tables (your database is under source control, right?). Another approach is to backup database before running the tests and then restore it once they finish.

Pavel Chuchuva
You know, that seems so obvious now that you said it :) I was hoping to avoid building up and tearing down the database all the time, but I guess I'm stuck with it.
Robin Clowers
You need to find a way to enable transaction support in LINQ to SQL so that it can join into the existing transaction scope.
Brad Wilson