C# 从通过单元测试项目调用的类库访问 App.Config 设置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1318423/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 15:05:30  来源:igfitidea点击:

Access App.Config Settings from Class Library Called through Unit Test Project

c#asp.netconfigurationnunitapp-config

提问by Yaakov Ellis

I have the following setup:

我有以下设置:

  • ASP.net 3.5 Web Site Project
  • C# Class Library with business logic
  • C# Class Library for unit testing
  • ASP.net 3.5 网站项目
  • 具有业务逻辑的 C# 类库
  • 用于单元测试的 C# 类库

The business logic library does all of the db access. It gets connection strings from the web.config file of the web site by accessing System.Configuration.ConfigurationManager.ConnectionStrings. When the library is called by the web site, this works fine, as the library looks for the config of the caller.

业务逻辑库执行所有数据库访问。它通过访问 System.Configuration.ConfigurationManager.ConnectionStrings 从网站的 web.config 文件中获取连接字符串。当网站调用库时,这工作正常,因为库查找调用者的配置。

I want to be able to test my business logic through the unit testing class library. I have put an App.config file in the root of the testing class library. From what I read, when the testing library calls data access procedures that are part of the business logic library, the connection settings from the App.config file of the testing library should be accessed and used. However, when I try to run my unit tests, I am getting errors back that indicate that the testing library's App.config file (and/or its contents) is not being accessed successfully.

我希望能够通过单元测试类库来测试我的业务逻辑。我在测试类库的根目录中放置了一个 App.config 文件。据我所知,当测试库调用作为业务逻辑库一部分的数据访问过程时,应该访问和使用测试库的 App.config 文件中的连接设置。但是,当我尝试运行我的单元测试时,我收到错误消息,表明测试库的 App.config 文件(和/或其内容)没有被成功访问。

My retrieval of the config properties (from within the business logic library) looks like this:

我对配置属性的检索(从业务逻辑库中)如下所示:

public SqlConnection MainConnection {
  get {
    string conn = "";
    try {
      conn = System.Configuration.ConfigurationManager.ConnectionStrings["connString"].ConnectionString;
    } catch {
      // might be calling from test project. Need to reference app settings
      conn = System.Configuration.ConfigurationManager.AppSettings["connString"];
    }
    return new SqlConnection(conn);
  }
}

When this is called from the website project, it works. From within the unit test, the conn variable is never set to anything (I have also tried System.Configuration.ConfigurationSettings.AppSettings, and using instead of with the same result). What do I need to do to make the business logic class library successfully retrieve the unit test class libraries settings, when called from within the NUnit GUI?

当从网站项目中调用它时,它会起作用。在单元测试中,conn 变量从未设置为任何内容(我也尝试过 System.Configuration.ConfigurationSettings.AppSettings,并使用而不是相同的结果)。当从 NUnit GUI 中调用时,我需要做什么才能使业务逻辑类库成功检索单元测试类库设置?

采纳答案by Yaakov Ellis

I just found the solution here. App.config is now being used properly when running my tests through the NUnit GUI.

我刚刚在这里找到了解决方案。通过 NUnit GUI 运行我的测试时,现在可以正确使用 App.config。

Apparently if you are using the NUnit GUI and add the assembly by going through Project > Add Assembly, it doesn't access the app.config. However, if you add the assembly to the NUnit project by dragging the dll from Windows Explorer into the NUnit GUI, then it will access the app.config.

显然,如果您使用 NUnit GUI 并通过“项目”>“添加程序集”添加程序集,它不会访问 app.config。但是,如果通过将 dll 从 Windows 资源管理器拖到 NUnit GUI 中来将程序集添加到 NUnit 项目,那么它将访问 app.config。

Alternatively, you can add the assembly through the GUI and then go in the NUnit GUI > Project > Edit, and set the Configuration File Name to the name of the configuration file (VS will set this to name.of.your.dll.config) and set the Project Base to the \bin\Debug directory of your project (these are the extra steps that are done in the background when you drag in the assembly vs adding it manually.

或者,您可以通过 GUI 添加程序集,然后进入 NUnit GUI > Project > Edit,并将 Configuration File Name 设置为配置文件的名称(VS 会将其设置为 name.of.your.dll.config ) 并将 Project Base 设置为项目的 \bin\Debug 目录(这些是在程序集拖入与手动添加时在后台完成的额外步骤。

回答by Jan Remunda

Try WebConfigurationManager.OpenWebConfiguration() method

尝试 WebConfigurationManager.OpenWebConfiguration() 方法

Configuration config = WebConfigurationManager.OpenWebConfiguration(@"x:\path\web.config");
            KeyValueConfigurationCollection appSettings = config.AppSettings.Settings;
            string connString = appSettings["connString"].Value;

回答by azheglov

I'd recommend changing the design such that your business-logic layer, instead of having the responsibility to locate configuration settings, is injectedwith them.

我建议更改设计,以便您的业务逻辑层被注入它们,而不是负责定位配置设置。

Your Web app could inject settings it reads from its Web.config file, while your unit test could inject different settings (e.g. connection string to a test database, etc.)

您的 Web 应用程序可以注入它从其 Web.config 文件读取的设置,而您的单元测试可以注入不同的设置(例如连接字符串到测试数据库等)

回答by lubos hasko

Just rename app.configto name.of.your.dll.config. It works for me.

只需重命名app.configname.of.your.dll.config. 这个对我有用。