ConfigurationManager.AppSettings在单元测试项目中返回Null

杰森·博伊德(Jason Boyd)

我有一个C#单元测试项目,app.config文件中有应用程序设置我正在测试存在于其他项目中的类。该类取决于ConfigurationManager.AppSettingsConfigurationManager.ConnectionStrings

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

该类检索连接字符串,没有任何问题。但是,当类尝试检索任何应用程序设置时,配置管理器始终返回null这里发生了什么?

编辑1

我认为尝试在测试项目中加载一些设置以查看会发生什么是一个好主意。在调用在外部项目中实例化该类的代码之前,我试图立即在单元测试中加载设置。结果相同,一无所有。我想暂时可以将其他项目排除在等式之外。

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

<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>

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

string test = System.Configuration.ConfigurationManager.AppSettings["Bing_Key"];
比尔·桑布朗

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

string test = Properties.Settings.Default.Bing_Key;

您可能需要获取定义项目设置文件所在位置的执行程序集,但请首先尝试。

编辑

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

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

ConfigurationManager.AppSettings在单元测试项目中返回Null

来自分类Dev

执行单元测试时,ConfigurationManager.GetSection(sectionName)返回null

来自分类Dev

ConfigurationManager.AppSettings(“ IsDebugMode”)返回null

来自分类Dev

ConfigurationManager.RefreshSection(“ appSettings”)无效

来自分类Dev

在项目本身中加载的ConfigurationManager

来自分类Dev

ConfigurationManager.AppSettings是做什么的?

来自分类Dev

ConfigurationManager.ConnectionStrings [0]。名称返回LocalSqlServer

来自分类常见问题

.NET Core 2.0中是否提供ConfigurationManager.AppSettings?

来自分类Dev

无法使用ConfigurationManager.AppSettings读取Web.config

来自分类Dev

UserManager中的ConfigurationManager连接字符串为null

来自分类Dev

在测试中,ConfigurationManager无法看到NUnit appSettings文件属性(链接的配置文件)

来自分类Dev

Visual Studio:使用RegEx将对ConfigurationManager.AppSettings的所有引用替换为CloudConfigurationManager.GetSetting

来自分类Dev

ConfigurationManager.AppSettings不适用于以编程方式创建的App.config文件

来自分类Dev

如何在自定义部分使用ConfigurationManager.AppSettings?

来自分类Dev

通过ConfigurationManager.AppSettings引用时找不到使用App.config键值

来自分类Dev

Azure环境上的ConfigurationManager

来自分类Dev

如何使用ConfigurationManager

来自分类Dev

ConfigurationManager.OpenMappedExeConfiguration的问题

来自分类Dev

WPF中的ConfigurationManager

来自分类Dev

ConfigurationManager.save()失败

来自分类Dev

WPF中的ConfigurationManager

来自分类Dev

RegistryKey Key.GetValue(“ keyName”)在单元测试c#项目中返回错误的值

来自分类Dev

.NET项目中单元测试和验收测试的实现

来自分类Dev

麻烦单元测试附加项目中的帮手

来自分类Dev

如何仅在Hybris项目中运行单元测试?

来自分类Dev

忽略本机VC ++项目中的单元测试

来自分类Dev

C ++堆损坏,但仅在单元测试项目中

来自分类Dev

多模块Spring Boot项目中的单元测试

来自分类Dev

无法从intellij中的Maven项目中执行单元测试

Related 相关文章

  1. 1

    ConfigurationManager.AppSettings在单元测试项目中返回Null

  2. 2

    执行单元测试时,ConfigurationManager.GetSection(sectionName)返回null

  3. 3

    ConfigurationManager.AppSettings(“ IsDebugMode”)返回null

  4. 4

    ConfigurationManager.RefreshSection(“ appSettings”)无效

  5. 5

    在项目本身中加载的ConfigurationManager

  6. 6

    ConfigurationManager.AppSettings是做什么的?

  7. 7

    ConfigurationManager.ConnectionStrings [0]。名称返回LocalSqlServer

  8. 8

    .NET Core 2.0中是否提供ConfigurationManager.AppSettings?

  9. 9

    无法使用ConfigurationManager.AppSettings读取Web.config

  10. 10

    UserManager中的ConfigurationManager连接字符串为null

  11. 11

    在测试中,ConfigurationManager无法看到NUnit appSettings文件属性(链接的配置文件)

  12. 12

    Visual Studio:使用RegEx将对ConfigurationManager.AppSettings的所有引用替换为CloudConfigurationManager.GetSetting

  13. 13

    ConfigurationManager.AppSettings不适用于以编程方式创建的App.config文件

  14. 14

    如何在自定义部分使用ConfigurationManager.AppSettings?

  15. 15

    通过ConfigurationManager.AppSettings引用时找不到使用App.config键值

  16. 16

    Azure环境上的ConfigurationManager

  17. 17

    如何使用ConfigurationManager

  18. 18

    ConfigurationManager.OpenMappedExeConfiguration的问题

  19. 19

    WPF中的ConfigurationManager

  20. 20

    ConfigurationManager.save()失败

  21. 21

    WPF中的ConfigurationManager

  22. 22

    RegistryKey Key.GetValue(“ keyName”)在单元测试c#项目中返回错误的值

  23. 23

    .NET项目中单元测试和验收测试的实现

  24. 24

    麻烦单元测试附加项目中的帮手

  25. 25

    如何仅在Hybris项目中运行单元测试?

  26. 26

    忽略本机VC ++项目中的单元测试

  27. 27

    C ++堆损坏,但仅在单元测试项目中

  28. 28

    多模块Spring Boot项目中的单元测试

  29. 29

    无法从intellij中的Maven项目中执行单元测试

热门标签

归档