C# 单元测试时无法读取应用设置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16955118/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
unable to read appsettings when unit testing
提问by New Developer
I have C# console application. One of its function reading appconfigvalue and do some work.
我有 C# 控制台应用程序。其功能之一是读取appconfig值并做一些工作。
string host = ConfigurationManager.AppSettings["Host"]
So I wrote NUNITtest for my console application. Unit testing project was created using class library.
所以我NUNIT为我的控制台应用程序编写了测试。单元测试项目是使用类库创建的。
But my unit test is fail now . Because it is not reading my app settings (indicates no app settings). What is the reason for this.
When I run my console app, it is reading app settings correctly.
但是我的单元测试现在失败了。因为它没有读取我的应用设置(表示没有应用设置)。这是什么原因。
当我运行我的控制台应用程序时,它正在正确读取应用程序设置。
采纳答案by aquaraga
You should have an app.config created for your unit test project. The app.config of your console application will not be consulted when you're running the unit tests.
您应该为您的单元测试项目创建了一个 app.config。当您运行单元测试时,不会查询控制台应用程序的 app.config。
回答by boniestlawyer
While you can define the app settings in another config file for your unit test project, unit testing to interfaces using dependency injection may help break down the areas that your unit tests will be covering into more manageable portions.
虽然您可以在另一个配置文件中为您的单元测试项目定义应用程序设置,但使用依赖注入对接口进行单元测试可能有助于将您的单元测试将覆盖的区域分解为更易于管理的部分。
So you could have your configuration interface like:
因此,您可以拥有如下配置界面:
public interface IConfiguration
{
public string Host { get; set; }
}
your class to test would accept an IConfiguration class as a parameter (usually to your constructor) like this:
您要测试的类将接受 IConfiguration 类作为参数(通常是您的构造函数),如下所示:
public class MyClass
{
IConfiguration _config;
public MyClass(IConfiguration config)
{
_config = config;
}
public void MyMethodToTest()
{
}
}
Then your test can use the interface to pass in the configuration rather than depending on an external configuration file that can potentially change and affect your unit test:
然后您的测试可以使用该接口来传递配置,而不是依赖于可能会更改和影响您的单元测试的外部配置文件:
[Test]
public void Testing_MyMethodToTest()
{
// arrange
var config = new Configuration { Host = "My Test Host" };
// act
new MyClass(config).MyMethodToTest();
// Add assertion for unit test
}
And your actual implementation would create your configuration class, load it with the value(s) from the appsettings and pass that into your implementation
您的实际实现将创建您的配置类,使用 appsettings 中的值加载它并将其传递到您的实现中
回答by Developer Sheldon
Another solution can be as simple as copy all the app settings json file into the build folder of your unit testing project if you are just use the same files. Sometimes in Rider, it just didnt copy, I have do it manually.
如果您只是使用相同的文件,另一种解决方案可以简单到将所有应用程序设置 json 文件复制到单元测试项目的构建文件夹中。有时在 Rider 中,它只是没有复制,我已经手动完成了。
回答by Walter
It seems like a Visual Studio version problem. I have opened the same project in different versions of Visual Studio.
这似乎是 Visual Studio 版本问题。我在不同版本的 Visual Studio 中打开了同一个项目。
- VS 2017 -> appsettings not found
- VS 2019 (16.4.0) -> appsettings found
- VS 2017 -> 未找到 appsettings
- VS 2019 (16.4.0) -> 找到 appsettings
VS2019 screenshot, as you can see the Test and the appsettings are placed in different folders.
VS2019 截图,如你所见,Test 和 appsettings 放在不同的文件夹中。


