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

NUnit appSettings file attribute (linked config file) is not seen by ConfigurationManager in test

我有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 用法。

我复制了你的用例,发现我的额外配置在 ASP.NET 站点的上下文中工作,但在我更改 Copy to Output Directory [=18] 之前,额外的 appSetting 在测试项目中为空=]到Copy Always

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