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

Resolving the "n timer(s) still in the queue" Error In Angular Unit Tests

Silent Renew and the "login_required" Error When Using oidc-client

How to Get Norton Security Suite Firewall to Allow Remote Desktop Connections in Windows

Fixing the "Please add a @Pipe/@Directive/@Component annotation" Error In An Angular App After Upgrading to webpack 4

How to Determine if a Column Exists in a DataReader