Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

app.configs and MSTest Project - null reference for a connection string

When I try to run Unit Tests (mstest) I run into this issue.
The line of code:

_mainCnStr = System.Configuration.ConfigurationManager.
               ConnectionStrings["main"].ConnectionString;

Comes back as a null reference

It doesn't do this in the main UI project when I run it. What is the right method to get this connection string setting seen by the Unit Test project? I tried embedded as a resource. I tried Copy Always. What is the right combination of settings that will fix this for me?

like image 710
BuddyJoe Avatar asked May 30 '09 20:05

BuddyJoe


2 Answers

One thing to watch with MSTest (from the IDE at least); it doesn't run the tests in the regular output (bin) folder, and it doesn't respect the project's file inclusions ("Copy to Output Directory"). You often need to explicitly tell it (MSTest) which files to put into the test area. You will need to include the "app.config" in this list; either via the testrunconfig ("Deployment"), or by adding an attribute ([DeploymentItem]) to the affected test fixtures.

like image 124
Marc Gravell Avatar answered Oct 16 '22 20:10

Marc Gravell


You should add an app.config to the unit test project. It won't automatically use the settings in UI application's app.config.

like image 31
mmx Avatar answered Oct 16 '22 21:10

mmx