Question
Is there any way to specify a connection for the transactionscope so that the effects of this "out-of-context" test can be rolled back?
Background
I'm using RestSharp to test WebAPI controllers. This is an integration test and tests by sending a request to the API, which propagates all the way down to the data layer through the repository and service layers. In other words, this is intentionally NOT a unit test.
The test creates an instance of restClient:
[TestInitialize()]
public virtual void TestInitialize()
{
_client = new RestClient("http://localhost:24144");
_client.CookieContainer = new System.Net.CookieContainer();
}
The client then makes some requests, totally ignorant of the data context (again, intentional):
// pseudocode
_client.DoThis();
_client.DoThat();
This test is run against a test database which is generated from EF code-first migrations. I have an empty seed method so the database is empty:
protected override void Seed(TestAppDbContext context)
{
SqlConnection.ClearAllPools();
context.Database.Delete();
context.Database.CreateIfNotExists();
}
The Problem
This is just one of many tests that I would like to run. Currently I have to drop/recreate the database between tests AND make sure that the Api Test gets run after the other tests. Since it doesn't know anything about the data context, I can't (or haven't been able to figure out how to) wrap all of the test code in a transaction to roll back.
What I've tried
- newing up a transactionscope in TestInitialize and disposing it in TestCleanup. This does nothing.
- running 'update-database' after this test just for kicks - but I always get 'Cannot drop database because it is currently in use' despite my call to ClearAllPools(). This isn't the issue that I'd like to focus on but thought it was worth mentioning.