0

I have two projects in Visual Studio, the core project which is a Windows Service Executable, and the Unit Test Project.

The core project has two original files broken out like this

File1.cs:

using static Proj.ConfigHelper;

namespace Proj
{
  class MyClass
  {
     (lots of code)
  }
}

File2.cs looks something like this.

namespace Proj
{
  static class ConfigHelper
  {
    public static NameValueCollection AppSettings { get { return ConfigurationManager.AppSettings; } }
    public static NameValueCollection CustomSection { get { return ConfigurationManager.GetSection("CustomSection") as NameValueCollection; } }
  }
}

Both of those classes are internal and made visible to the unit test project via InternalsVisibleToAttribute.

Inside of the UnitTest project which is a discrete project within the same solution (and so it has its own app.config), calling ConfigHelper.AppSettings results in a 0-item collection, and calling ConfigHelper.CustomSection results in null. If I attempt to unit test a method within File1.cs that depends on those settings, they run as default values as if they were not configured at all. I don't quite understand why this is happening. Can anyone help me understand what I did wrong? It seems as though the ConfigHelper is not loading the App.Config for its own project.

The app.config for the Windows Service Project is set to "always copy" and the app.config for the unit test project is set to "never copy"

user3657661
  • 306
  • 3
  • 13
  • It's not clear by your description, are the settings also in the test project's config? The tests will not read from another project's configuration – Crowcoder Dec 19 '19 at 23:50

1 Answers1

0

The test will use its own config so you need to mirror it. There are work runarounds: Can a unit test project load the target application's app.config file?

azpc
  • 500
  • 9
  • 11