简体   繁体   中英

Unit Testing C# Assembly with App.Config returns exception

I have a 3 project solution that comprises of my DAL, an assembly, and my unit test project. I am using ReSharper.

The following field, in my DAL, returns NULL for the object SectionHandler:

  public static DatabaseFactorySectionHandler SectionHandler =
        (DatabaseFactorySectionHandler) ConfigurationManager.GetSection("DatabaseFactoryConfiguration");

This is a line from a Factory class. Another class that inherits from it attempts to initialize the field from its constructor.

I remembered I needed to add my App.config file to my Unit Test project (it was pointing to Machine.config). However, it hasn't fixed the problem. What I get instead when attempting to run my test is:

 > failed: Configuration system failed to initialize System.Configuration.ConfigurationErrorsException: Configuration > system failed to initialize ---> > System.Configuration.ConfigurationErrorsException: Unrecognized > configuration section section. > (C:\\projects\\MyAssembly.Tests\\bin\\Debug\\MyAssembly.Tests.dll.temp.config > line 3) 

What I don't understand is the naming of the config file. In my directory I can see App.config has been renamed correctly (MyAssembly.Tests.dll.config), but there is no sign of temp.config.

Here's my app.config (this is what is currently in my Test project, there are no other App.configs in the solution)

    <?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="DatabaseFactoryConfiguration" type="MyClass.DatabaseFactorySectionHandler, MyClass.DBConnector, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
  </configSections>
  <connectionStrings>
    <clear/>
    <add name="Connector1" providerName="MyProvider" connectionString="myConnString" />
    <add name="DB2Connector" providerName="IBM.Data.DB2" connectionString="myConnString2" />
  </connectionStrings>

  <DatabaseFactoryConfiguration Name="MyClassHere" ConnectionStringName="Connector1" />
  <DatabaseFactoryConfiguration Name="MyOtherClassHere" ConnectionStringName="DB2Connector" />
</configuration>

Why can't I get the unit test project to recognize my config file? Thanks.

原来,该问题与app.config文件中具有多个DatabaseFactoryConfiguration元素有关。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM