locked
Using VS Unit Testing Framework to test assembly containing Linq to SQL classes RRS feed

  • Question

  • When I use the VS unit testing framework to test an assembly containing a parameterized connection string, the path to my .mdf is all screwed up. When I run my unit test, I get the following error message:

    Test method QuickReceipt.Test.DatabaseTest.TestConnect threw exception:  System.Data.SqlClient.SqlException: An attempt to attach an auto-named database for file C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\database\QuickReceipt.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share..

    My assembly-under-test contains a service-based database (.mdf) and linq to sql classes (.dbml) that uses the .mdf. The connection string is contained in my app.config file, and looks like this:

    <add name="QuickReceipt.Properties.Settings.QuickReceiptConnectionString1"

    connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\database\QuickReceipt.mdf;Integrated Security=True;User Instance=True"

    providerName="System.Data.SqlClient" />

    My unit test is contained in a seperate test project and looks like this:

    [TestMethod]

    public void TestConnect() {

        QuickReceiptLINQDataContext db = new QuickReceiptLINQDataContext();

    }


    It seems that |DataDirectory| is getting replaced with "C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE", when actually I want it to point to my mdf file. My temporary workaround has been to hardcode a connection string into my test project that contains an absolute path to the mdf. This project lives in source control and is shared by several people, so it shouldn't contain any absolute paths.

    How do I configure my test project or my assembly-under-test so that it points to the desired mdf file without using any absolute paths?

    Should I be using [DeploymentItem()] ? I don't think I want to be coyping mdf files around, do I? I thought [DeploymentItem()] was for test data, not for application data.

    • Moved by Michael Sun [MSFT]Microsoft employee Thursday, August 14, 2008 9:45 AM Off Topic for Visual C# General (Moved from Visual C# General to Off-Topic Posts (Do Not Post Here))
    Monday, August 11, 2008 11:44 PM

Answers