ANSWER: I wasn't clearing my ConnectionPools.
The solved scenario is:
SetUp:
Test:
TearDown:
SqlConnection.ClearAllPools();
Original question is below the fold.
Hi All
The test scenario is:
SetUp:
Test:
TearDown:
Everything's going fine until I get to the TearDown phase. I always get the following error:
Cannot drop database "TEMP_NUnit" because it is currently in use.
This is confusing me, because I'm closing (explicitly calling conn.Close in finally statements) and disposing (via using statement) all my DbConnection objects correctly. There shouldn't be anything running on the "TEMP_NUnit" database by the time the TearDown comes around.
If I close NUnit and then re-open it, step 2 of the SetUp always works first go.
I'm not sure what I'm doing wrong here. Any pointers in the right direction would be appreciated.
System databases cannot be dropped.
Connect to SQL Server and right-click the database you want to remove. Click delete command and the following screen will appear. Click OK to remove the database (in this example, the name is Testdb as shown in the above screen) from MS SQL Server.
To delete a database In Object Explorer, connect to an instance of the SQL Server Database Engine, and then expand that instance. Expand Databases, right-click the database to delete, and then click Delete. Confirm the correct database is selected, and then click OK.
Unit tests should never connect to a database. By definition, they should test a single unit of code each (a method) in total isolation from the rest of your system. If they don't, then they are not a unit test.
Ah, but you forgot to do this little thing before dropping the database:
SqlConnection.ClearAllPools();
By default the connection pool will maintain a connection to the database, even if you closed it before. Doing SqlConnection.ClearAllPools()
will actually force all connections to be closed. You can then connect to master
and drop the temp database.
It has caused me quite an amount of grief before :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With