Troubleshooting Database Connectivity Issues When Unit Testing With MSTest

I was attempting to run some unit tests today for an ASP.NET MVC project I'm working on when I ran into the following fun little exception:

Test method BlogMVC.Tests.Controllers.HomeControllerTest.Index threw exception:
System.Data.EntityException: The underlying provider failed on Open. ---> System.Data.SqlClient.SqlException: An attempt to attach an auto-named database for file C:\Users\Alan\Documents\Visual Studio 2010\Projects\BlogMVC\TestResults\Alan_QUIGON 2012-05-15 16_54_57\Out\BlogMVC.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.

The issue is that each unit test is run in it's own individual directory, and in my case the unit tests needed my database files, which were not being deployed to the unit test directory.

Fortunately this is easy to correct. To fix this issue, do the following:

  1. Right-click on the solution (not the project) and select "Add -> New Item".
  2. Choose "Test Settings" from the installed Test Settings template, then click "Add".
  3. Your new test settings file should appear under the Solution Items folder of your solution. Double-click to open it.
  4. Select "Deployment" from the list on the left. This will show an area on the right where you can choose which folders and/or files you'd like deployed for you tests. In my case, I selected the App_Data directory.

That's all there is to it. :)

Comments

Popular posts from this blog

A Generic Method Using HttpClient to Make a Synchronous Get Request

The Cause and Solution for the "System.Runtime.Serialization.InvalidDataContractException: Type 'System.Threading.Tasks.Task`1[YourTypeHere]' cannot be serialized." Exception

How To Mock Out Child Components In Unit Tests of Angular 2 Code