2

I am trying to run Unit Tests using the Entity Framework.

When I run using debug or release I set my connection string like so:

#if RELEASE
        public DataContext()
            : base("Release")
        {
        }
#else
        public DataContext()
            : base("Debug")
        {
        }
#endif

My Connection Strings in Web.Config look like:

 <connectionStrings>
    <add name="Debug" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\mydatabase.mdf;Initial Catalog=mydatabase;Integrated Security=True" providerName="System.Data.SqlClient" />
    <add name="Release" connectionString="MultipleActiveResultSets=true;Net=dbmssocn;Server=my.site.com;Database=mydatabase;User ID=myuser;Password=mypass;" providerName="System.Data.SqlClient" />
  </connectionStrings>

This works great when I simply run my Asp.Net MVC site.

But when I go to run Unit Tests, it looks in the bin directory instead of using the Debug connection string with the debug database in my App_Data directory, even if the project is build against the Debug configuration before running the tests.

The error I get is:

System.Data.Entity.Core.EntityException: The underlying provider failed on Open. ---> System.Data.SqlClient.SqlException: Cannot attach the file 'D:\Development\Projects\myproject\MyProject\bin\mydatabase.mdf

Why is it looking in the bin directory instead of using the App_Data path defined in the connection string?

ANSWER:

The answer was to set the data directory in the App Domain like this in the Test Fixture Setup in my Base Test Class:

public class TestBase
{
    [TestFixtureSetUp]
    public void Setup()
    {
        var baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
        var appDataDirectory = Path.Combine(baseDirectory.Replace("\\bin", ""), "App_Data");

        AppDomain.CurrentDomain.SetData("DataDirectory", appDataDirectory);
    }
}
4
  • what unit test are you runned? missing info. anyway your connection string should not be involved in unit testing, you have to mock your repository Commented Jun 19, 2014 at 6:35
  • Maybe this can help: stackoverflow.com/questions/1166883/… Commented Jun 19, 2014 at 6:38
  • My Unit Test calls a Manager class that creates an instance of DataContext and executes a simple find. Why wouldn't the connection string be involved? It's calling the constructor. Also, what does 'mock your repository' mean? Commented Jun 19, 2014 at 6:39
  • @abc unit testing involve isolated code without dependency, so your test should not have dependency to Devices ,File System or Database availability, its a wrong approach to unit testing Commented Jun 19, 2014 at 7:45

1 Answer 1

1

For a Unit-Test project, DataDirectory is not set automatically. Try to set it manually yourself:

var appDataDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "../../App_Data");

AppDomain.CurrentDomain.SetData("DataDirectory", appDataDir);

Make sure you're invoking the above code before initializing EF context.

Sign up to request clarification or add additional context in comments.

1 Comment

Woo hoo! This worked perfect! Thanks. That was a pain in the ass. I'm actually going to edit this just a bit to what actually worked in the end, bit it is based on your answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.