【发布时间】:2020-04-12 13:18:13
【问题描述】:
我在 Visual Studio 中有两个项目,一个是 Windows 服务可执行文件的核心项目,另一个是单元测试项目。
核心项目有两个原始文件是这样分解出来的
文件1.cs:
using static Proj.ConfigHelper;
namespace Proj
{
class MyClass
{
(lots of code)
}
}
File2.cs 看起来像这样。
namespace Proj
{
static class ConfigHelper
{
public static NameValueCollection AppSettings { get { return ConfigurationManager.AppSettings; } }
public static NameValueCollection CustomSection { get { return ConfigurationManager.GetSection("CustomSection") as NameValueCollection; } }
}
}
这两个类都是内部的,并且通过 InternalsVisibleToAttribute 对单元测试项目可见。
在 UnitTest 项目内部,这是同一解决方案中的一个离散项目(因此它有自己的 app.config),调用 ConfigHelper.AppSettings 会导致 0 项集合,调用 ConfigHelper.CustomSection 会导致 null。如果我尝试对依赖于这些设置的 File1.cs 中的方法进行单元测试,它们会作为默认值运行,就好像它们根本没有配置一样。我不太明白为什么会这样。谁能帮我理解我做错了什么?似乎 ConfigHelper 没有为自己的项目加载 App.Config。
Windows 服务项目的 app.config 设置为“始终复制”,单元测试项目的 app.config 设置为“从不复制”
【问题讨论】:
-
你的描述不清楚,测试项目的配置中也有设置吗?测试不会从另一个项目的配置中读取
标签: c# unit-testing windows-services app-config