I am trying to write a test case using xUnit.net and the AutoRollback attribute provided by the xunit extensions.
我正在尝试使用xUnit.net和xunit扩展提供的AutoRollback属性编写测试用例。
My test case goes like this
我的测试用例是这样的
[Fact, AutoRollback]<br>
public void TestCase()<br>
{
// insert into data table
repository.Insert(data);
// spawn a new thread and read data which you just inserted in the data table
Task.Factory.StartNew(action_to_read_data);
}
This test case fails and throws a connection timeout exception while reading the data in the new thread. The problem I have found is that the test case starts a transaction because of the AutoRollback attribute and while Inserting the data it locks the table for rollback at the end. The new thread spawned by the test case cannot read data from the same table because it is locked by the parent thread. I can read the data in the same thread though.
此测试用例失败,并在读取新线程中的数据时抛出连接超时异常。我发现的问题是测试用例由于AutoRollback属性而启动一个事务,而在插入数据时它会锁定表以便在结束时进行回滚。由测试用例生成的新线程无法从同一个表读取数据,因为它被父线程锁定。我可以在同一个线程中读取数据。
Anyone have a solution? I want to run multiple threads reading the data inserted above.
有人有解决方案吗?我想运行多个线程来读取上面插入的数据。
1 个解决方案
#1
1
What I have found so far is that you can't use another thread which interacts with the same database in your unit test while using the AutoRollback feature. I ended up removing that attribute from my test case and keeping track of all objects inserted by the test case myself.
到目前为止,我发现在使用AutoRollback功能时,您不能使用与单元测试中的同一数据库交互的另一个线程。我最终从我的测试用例中删除了该属性,并自己跟踪测试用例插入的所有对象。
#1
1
What I have found so far is that you can't use another thread which interacts with the same database in your unit test while using the AutoRollback feature. I ended up removing that attribute from my test case and keeping track of all objects inserted by the test case myself.
到目前为止,我发现在使用AutoRollback功能时,您不能使用与单元测试中的同一数据库交互的另一个线程。我最终从我的测试用例中删除了该属性,并自己跟踪测试用例插入的所有对象。