This question is for someone experienced with both SQL Server Express and NUnit. I think the answer might be simple, but I would like to understand what is going on and how to prevent it. I have a test class with a TestFixtureSetUp section that calls a method to drop and re-build my database from scratch, including tables for the Membership and Roles provider. This code works fine. After TestFixtureSetUp runs, I have a test to verify that there are 3 records in the Membership database which were added when the database was re-built. The code looks like this:
[Test]
public void MembershipUsersTest()
{
int expectedNumberOfUsers = 3;
MembershipUserCollection users = System.Web.Security.Membership.GetAllUsers();
Assert.AreEqual(expectedNumberOfUsers, users.Count);
}
When this test runs along with the TextFixtureSetUp code, I get this error:
System.Data.SqlClient.SqlException : Cannot open user default database. Login failed. Login failed for user 'TERRY-COMPUTER\Terry'.
However, if I re-run this test but block the TestFixtureSetUp code that re-builds the database, the test passes fine.
I'm guessing it's just a simple matter of NUnit somehow locking access to the database when the database is rebuilt, but I am not understanding why this is happening. My code for re-generating the database uses a "using" construct, so the connnections should be closing properly.
using (SqlConnection conn = new SqlConnection(connectionString))
{
FileInfo file = new FileInfo(fullScriptPath);
string script = file.OpenText().ReadToEnd();
Server server = new Server(new ServerConnection(conn));
server.ConnectionContext.ExecuteNonQuery(script);
}
Any insights into why I am getting this error and how I can avoid it and still run my Membership test right after I re-build the db
One additional piece of information that might be important. The code for rebuilding the databases uses the following connection string:
@"Data Source=.\SQLExpress;Integrated Security=True"
Whereas the Membership provider uses the following connection string which has a hard-coded path to the database:
"Data Source=.\SQLExpress;Integrated Security=True;User Instance=True;AttachDBFilename=C:\MyApps\MyAppsWeb\App_Data\MyApp.mdf"