ConfigurationManager.AppSettings 在单元测试项目中返回 Null [英] ConfigurationManager.AppSettings Returns Null In Unit Test Project

查看:35
本文介绍了ConfigurationManager.AppSettings 在单元测试项目中返回 Null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 app.config 文件中有一个带有应用程序设置的 C# 单元测试项目.我正在测试一个存在于不同项目中的类.该类依赖于 ConfigurationManager.AppSettingsConfigurationManager.ConnectionStrings.

I have a C# unit test project with application settings in the app.config file. I am testing a class that exists in a different project. That class depends on both, ConfigurationManager.AppSettings and ConfigurationManager.ConnectionStrings.

被测试类所在的项目没有 app.config 文件.我会认为因为该类是在单元测试项目的上下文中实例化的,所以它将使用单元测试项目的 app.config 文件.事实上,连接字符串似乎就是这种情况.

The project that the class being tested resides in does not have an app.config file. I would have thought that because the class is being instantiated in the context of the unit test project that it would use the unit test project's app.config file. Indeed, that does seem to be the case for the connection string.

该类可以毫无问题地检索连接字符串.但是,当该类尝试检索任何应用程序设置时,配置管理器总是返回 null.这是怎么回事?

The class retrieves the connection string without any issues. However, when the class tries to retrieve any application settings the configuration manager always returns null. What is going on here?

编辑 1

我想也许尝试在测试项目中加载一些设置看看会发生什么是个好主意.我尝试在调用在外部项目中实例化类的代码之前立即加载单元测试中的设置.结果一样,没什么.我想我可以暂时从等式中排除另一个项目.

I thought maybe it would be a good idea to try load some settings in the test project to see what happens. I tried to load the setting in the unit test immediately before calling the code that instantiates the class in the external project. Same result, nothing. I guess I can exclude the other project from the equation for the time being.

这是我的配置文件的摘录:

Here is an excerpt from my config file:

<configSections>
  <sectionGroup name="applicationSettings"
                type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
    <section name="MyNamespace.Properties.Settings"
             type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
             requirePermission="false" />
  </sectionGroup>
</configSections>

...

<applicationSettings>
  <MyNamespace.Properties.Settings>
    <setting name="Bing_Key"
             serializeAs="String">
      <value>...</value>
    </setting>
  </MyNamespace.Properties.Settings>
</applicationSettings>

这是我尝试加载设置的方式:

and here is how I am attempting to load the setting:

string test = System.Configuration.ConfigurationManager.AppSettings["Bing_Key"];

推荐答案

您提到了项目属性中的设置.看看你是否可以通过这种方式访问​​设置:

You mentioned settings in the project properties. See if you can access the setting this way:

string test = Properties.Settings.Default.Bing_Key;

您可能需要获取定义项目设置文件的执行程序集,但请先尝试此操作.

You may need to get the executing assembly of where the project settings file is defined, but try this first.

编辑

当使用 Visual Studio 的项目设置文件时,它会将内容添加到您的 app.config 并创建 app.config(如果它不存在).ConfigurationManager 无法触及这些设置!您只能使用上述静态方法来获取这些特定生成的 project.settings 文件.如果你想使用 ConfigurationManager,你需要手写你的 app.config.像这样添加您的设置:

When using Visual Studio's project settings file, it adds stuff to your app.config and creates the app.config if it is not present. ConfigurationManager CAN'T touch these settings! You can only get to these specific generated project.settings file from using the above static method. If you want to use ConfigurationManager, you will need to hand write your app.config. Add your settings to it like so:

<appSettings>
  <add key="bing_api" value="whatever"/>
</appSettings>

这篇关于ConfigurationManager.AppSettings 在单元测试项目中返回 Null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆