【问题标题】:NUnit appSettings file attribute (linked config file) is not seen by ConfigurationManager in testConfigurationManager 在测试中看不到 NUnit appSettings 文件属性(链接的配置文件)
【发布时间】:2016-02-02 13:59:43
【问题描述】:

我有 NUnit 测试(版本 2.6.4)测试。它使用ConfigurationManager.AppSettings["foo"]app.config 文件(位于测试项目中)中检索配置设置。这是我的App.config 文件:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings file="bar.config">
    <add key="thisSettingIsVisible" value="yes, indeed"/>
  </appSettings>
</configuration>

这是 bar.config 文件:

<appSettings>
  <add key="foo" value="this setting isn't visible"/>
</appSettings>

我正在使用 ReSharper 10 测试运行程序来执行测试。 bar.config 文件被复制到bin/Debug 目录。事实上,该配置在一段时间前还有效,但停止了。任何线索可能是错误的?

现在,我想出了一个解决方法,但我对这个解决方案不满意:

private static void InitializeAppSettings()
{
    var exeAssembly = System.Reflection.Assembly.GetExecutingAssembly();
    var assemblyName = exeAssembly.GetName().Name + ".dll";
    var testDllFolder = new Uri(System.IO.Path.GetDirectoryName(exeAssembly.CodeBase)).LocalPath;
    var openExeConfiguration = ConfigurationManager.OpenExeConfiguration(Path.Combine(testDllFolder, assemblyName));
    foreach (var setting in openExeConfiguration.AppSettings.Settings.AllKeys)
    {
        ConfigurationManager.AppSettings[setting] = openExeConfiguration.AppSettings.Settings[setting].Value;
    }
}

顺便说一句。我无法从现有的遗留代码中抽象出 ConfigurationManager 用法。

【问题讨论】:

  • 您使用的是哪种 ReSharper 10 版本?我们修复了关于在 R# 10.0.2 build youtrack.jetbrains.com/issue/RSRP-450410 中复制配置文件的类似问题。
  • 我一直在使用 ReSharper 10.0.0,升级到 R# 10.0.2 后问题消失了。您可以将其发布为答案吗?

标签: c# .net unit-testing nunit


【解决方案1】:

我复制了您的用例,发现我的附加配置在 ASP.NET 站点的上下文中工作,但附加的 appSetting 在测试项目中为空,直到我将 Copy to Output Directory 属性更改为 Copy Always

【讨论】:

  • 不,事实并非如此。我明确写过:“bar.config 文件被复制到 bin/Debug 目录”。
  • 请注意,如果您安装了最新版本的 ReSharper,此答案可能是正确的。
【解决方案2】:

如果您使用 R# 10.0.0 或 R# 10.0.1 - 它是 a known issue for such builds,并且已在 R# 10.0.2 版本中修复。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-28
    • 2018-11-24
    • 1970-01-01
    • 2019-05-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多