1

I have added new unit test project in VS 2015 for a method in my .net solution, while i run test case, the web config app settings values are null and unable to complete test, Actually the config key values are available and accessible during web app run, but during unit test the values are null,

can anyone please help me out.

Thanks in Advance

ravi
  • 21
  • 5
  • Test project needs `app.cofig` with similar settings present. Unit test is run in separate app domain so needs it's own config file. – Nkosi Dec 14 '17 at 11:18
  • In theory, you should not read any file during unit test. [Please check](https://stackoverflow.com/questions/377423/best-practices-for-file-system-dependencies-in-unit-integration-tests). You can use [Microsoft Fakes](https://msdn.microsoft.com/en-us/library/hh549176.aspx) to isolate your code from system components (or any other third party assembly). – Mutlu Kaya Dec 14 '17 at 12:06
  • Thank you so much NKosi , its working – ravi Dec 14 '17 at 12:14

1 Answers1

0

Test project needs app.cofig with similar settings present as in web project being tested.

Unit test is run in a separate app domain so needs it's own config file.

The ConfigurationManager reads the config file of the current app domain being run so create an app.config file for the test project and copy the desired configuration settings over to allow the tests to be exercised as expected.

This issue also exposes how your code is tightly coupled to implementation concerns in terms of the using the ConfigurationManager and you should consider abstracting configuration access so that they can be mocked/replaced to allow unit tests to be done in isolation.

Nkosi
  • 235,767
  • 35
  • 427
  • 472