7

我正在尝试对我的 ASP.NET 应用程序进行一些自动化 Web 测试。我希望使用 Xunit.net 扩展中的 AutoRollback 属性来撤消测试期间所做的任何数据库更改。AutoRollback 使用 TransactionScope 在测试之前启动事务并在之后回滚。

当我尝试在事务期间访问我的 Web 应用程序时,它总是超时。看起来这应该可行,有什么想法吗?这是我的测试:

[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"));
}
4

1 回答 1

5

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.

于 2009-06-16T23:49:08.090 回答