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

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

问题描述

我有一个C#单元测试项目,在app.config文件中具有应用程序设置.我正在测试存在于其他项目中的类.该类同时取决于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天全站免登陆