[英]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. 我有3个项目解决方案,其中包括我的DAL,程序集和单元测试项目。 I am using ReSharper.
我正在使用ReSharper。
The following field, in my DAL, returns NULL for the object SectionHandler: 在我的DAL中,以下字段为对象SectionHandler返回NULL:
public static DatabaseFactorySectionHandler SectionHandler =
(DatabaseFactorySectionHandler) ConfigurationManager.GetSection("DatabaseFactoryConfiguration");
This is a line from a Factory class. 这是Factory类的一行。 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). 我记得我需要将App.config文件添加到我的单元测试项目中(它指向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.
在我的目录中,可以看到App.config已正确重命名(MyAssembly.Tests.dll.config),但是没有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) 这是我的app.config(这是我的Test项目中当前的内容,解决方案中没有其他App.configs)
<?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元素有关。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.