Terry McGinty


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"




Re: SQL Server Express error when using NUnit

Jens K. Suessmeyer


The connection string you are opening and which throws the error, does this one contains a Initial Catalog setting If yes, change the database to the master database which is accessible to every user who has access to the server. If the user (in your case 'TERRY-COMPUTER\Terry') has a default database assigned which was dropped in the meantime, this error can occur. So set the default database for the user again using either the GUI or sp_defaultdb. If you cannot do this or have no access to the user profile use the "initial Catalog=master" extension within the connectionstring.

Jens K. Suessmeyer

---
http://www.sqlserver2005.de
---







Re: SQL Server Express error when using NUnit

Terry McGinty

Thanks Jens. That got me closer, but I am now getting a different error. I set the default database for user ¡®TERRY-COMPUTER\Terry¡¯ to master (using SQL Server Management Studio in the \Security\Login folder) and that seemed to get rid of the default database error. But I am now getting the following error:

MyApp_Tests.DatabaseMembershipTestFixture.MembershipUsersTest : System.Data.SqlClient.SqlException : Unable to open the physical file "C:\MyApps\MyAppsWeb\App_Data\MyApp.mdf". Operating system error 32: "32(The process cannot access the file because it is being used by another process.)".

An attempt to attach an auto-named database for file C:\MyApps\ MyAppsWeb\App_Data\MyApp.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.

Again, this MembershipUsersTest runs successfully if I comment out the code that re-builds the database. I¡¯m not clear why the code to rebuild the database would lock up the database if I have wrapped that code in a "using" construct (ensuring that the connections close). Based on this error however, it seems like NUnit is somehow keeping a link to this database. Any insights would be appreciated.






Re: SQL Server Express error when using NUnit

Jens K. Suessmeyer

Well, is the database server attached or user-attached. Seems that another process has still its hand on the database. Perhpas you might also could provide your detailed way on how you experiencing the problem. I am not quite sure if I got your point when that happened.

Jens K. Suessmeyer

---
http://www.sqlserver2005.de
---






Re: SQL Server Express error when using NUnit

Terry McGinty

Thanks Jens.

There are really only 2 things going on in my test code. I just re-create my database and tables (using the "using" construct as shown above) and then test the resulting Membership table to confirm that it has 3 users. When I run the test, the database and tables are rebuilt successfully, but the Membership test fails and gives me the error I listed in my last post. But if I comment out the TestFixtureSetUp code that re-builds the database, and then re-run the test, then the Membership test passes.

This makes me think that the only possibility is that NUnit is holding on to the process when I include the rebuild script in my test fixture. But I don't know why this would be if I have wrapped that rebuild code in a "using" statement.

Any insights would be appreciated.

Terry