Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Failing unit tests due to missing .config file

I am new to unit testing, so I am probably misunderstanding something big, but I have been asked to create some unit tests for my WCF service. It's a very simple service that executes a stored procedure and returns the result. The second line in my operation is this:

string conn = ConfigurationManager
    .ConnectionStrings["AtlasMirrorConnectionString"].ConnectionString;

Everything works fine when deploying the service, but under unit testing, it seems that the config file becomes invisible. ConfigurationManager.ConnectionStrings["AtlasMirrorConnectionString"] becomes a null reference and throws accordingly.

How do I include my config file in the tests? Right now, the only behavior I'm able to test is the handling of missing config files, which is not terribly useful.

like image 799
recursive Avatar asked Sep 01 '11 14:09

recursive


2 Answers

asked again and again and again and answered by me last week and this week as well :)

if you have your unit tests in another project (VS generated test project, class library etc...) just create an app config for that unit test project and put the same configuration keys as you have in the project which works.

of course I am simplifying because you could absolutely want to customize those keys with specific test values, but as a start copy what works then customize in case you want to point to another database, machine etc... :)

like image 165
Davide Piras Avatar answered Sep 29 '22 07:09

Davide Piras


If you want your unit test to always have the same values as your project then you can use the following line as a post-build event in the test project

copy /Y "$(SolutionDir)ProjectName\App.config" "$(TargetDir)TestProjectName.dll.config"
like image 35
zlsmith86 Avatar answered Sep 29 '22 06:09

zlsmith86