无法使用 ConfigurationManager 在 C# .NET Core 单元测试项目中读取 app.config [英] Can't read app.config in C# .NET Core unit test project with ConfigurationManager

查看:38
本文介绍了无法使用 ConfigurationManager 在 C# .NET Core 单元测试项目中读取 app.config的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个简单的单元测试项目来读取 app.config 文件.目标框架是 Core 2.0.我还创建了一个 Core 2.0 控制台应用程序,对自己进行完整性检查以确保我没有做任何奇怪的事情(在 .NET 4.6.1 单元测试项目中按预期通过了相同的测试).

I've created a simple unit test project to read an app.config file. Target framework is Core 2.0. I also created a Core 2.0 console app, to sanity-check myself to make sure I wasn't doing anything weird (same test passed as expected in a .NET 4.6.1 unit test project).

控制台应用程序读取 app.config 正常,但单元测试方法失败,我无法弄清楚原因.两者都使用相同 app.config 的副本(未添加为链接)并且都安装了 System.Configuration.ConfigurationManager v4.4.1 NuGet 包.

The console app reads the app.config fine, but the unit test method fails and I cannot figure out why. Both are using a copy of the same app.config (not added as a link) and both have the System.Configuration.ConfigurationManager v4.4.1 NuGet package installed.

App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="Test1" value ="This is test 1."/>
    <add key="Test2" value ="42"/>
    <add key="Test3" value ="-42"/>
    <add key="Test4" value="true"/>
    <add key="Test5" value="false"/>
    <add key="Test6" value ="101.101"/>
    <add key="Test7" value ="-1.2345"/>
  </appSettings>
</configuration>

单元测试

using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Configuration;

namespace ConfigTest
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod()]
        public void ConfigTest()
        {
            foreach (string s in ConfigurationManager.AppSettings.AllKeys)
            {
                System.Console.WriteLine(s);
                System.Diagnostics.Debug.WriteLine(s);
            }

            //AllKeys.Length is 0? Should be 7...
            Assert.IsTrue(ConfigurationManager.AppSettings.AllKeys.Length == 7);
        }
    }
}

控制台应用

using System;
using System.Configuration;

namespace ConfigTestApp
{
    class Program
    {
        static void Main(string[] args)
        {
            foreach (string s in ConfigurationManager.AppSettings.AllKeys)
            {
                Console.WriteLine(s);
                System.Diagnostics.Debug.WriteLine(s);
            }

            //Outputs 7 as expected
            Console.WriteLine(ConfigurationManager.AppSettings.AllKeys.Length);
        }
    }
}  

鉴于我对整个 .NET Core 世界仍然很陌生,我在这里做的事情是否完全错误?此刻我有点发疯了......

Given that I'm still pretty new to the whole .NET Core world, am I doing something totally incorrect here? I sort of just feel crazy at the moment...

推荐答案

查看 github 问题的评论,我找到了一个可以进入 msbuild 文件的解决方法...

Looking through the github issue's comments, I found a work around that can go in the msbuild file...

<Target Name="CopyCustomContent" AfterTargets="AfterBuild">
  <Copy SourceFiles="app.config" DestinationFiles="$(OutDir)	esthost.dll.config" />
</Target>

这使得在将配置数据移植到 json 配置文件之前,可以更轻松地验证 .NET Core 下的现有测试.

This makes it easier to verify existing tests under .NET Core before porting the configuration data over to json configuration files.

编辑

如果在 Resharper 下运行,则前面的答案不起作用,因为 Resharper 代理程序集,因此您需要

If running under Resharper, the previous answer doesn't work as Resharper proxies the assembly, so you need

<Target Name="CopyCustomContent" AfterTargets="AfterBuild">
  <Copy SourceFiles="app.config" DestinationFiles="$(OutDir)ReSharperTestRunner64.dll.config" />
</Target>

这篇关于无法使用 ConfigurationManager 在 C# .NET Core 单元测试项目中读取 app.config的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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