Unit tests failed due to missing .config file

I am new to unit testing, so I probably misunderstand something big, but I was asked to create some unit tests for my WCF service. This is a very simple service that executes a stored procedure and returns a result. The second line in my operation:

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

Everything works fine when deploying the service, but with unit testing it seems that the configuration file is becoming invisible. ConfigurationManager.ConnectionStrings["AtlasMirrorConnectionString"] becomes a null reference and changes accordingly.

How to include my configuration file in tests? Right now, the only thing I can check is handling missing configuration files, which is not very useful.

+6
source share
3 answers

asked again and again and again and answered me last week and this week: :)

if you have your unit tests in another project (VS, created test project, class library, etc.), just create the application configuration for this unit test project and put the same configuration keys as in the project that works .

of course, I simplify, because you can absolutely tune in to these keys with specific test values, but as an initial copy what works, then configure it if you want to specify a different database, machine, etc ....: )

+5
source

You will need to decorate the test class or DeploymentItemAttribute method to deploy the configuration file in the test directory.

Use something like this on your TestClass (this assumes you have a copy of app.config local for your test classes):

 [DeploymentItem("app.config")] 
+3
source

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

 copy /Y "$(SolutionDir)ProjectName\App.config" "$(TargetDir)TestProjectName.dll.config" 
+3
source

Source: https://habr.com/ru/post/896413/


All Articles