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

查看:454
本文介绍了无法使用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)\testhost.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.

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

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