从通过单元测试项目调用的类库访问 App.Config 设置 [英] Access App.Config Settings from Class Library Called through Unit Test Project

查看:33
本文介绍了从通过单元测试项目调用的类库访问 App.Config 设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下设置:

  • ASP.net 3.5 网站项目
  • 具有业务逻辑的 C# 类库
  • 用于单元测试的 C# 类库

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

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.

我希望能够通过单元测试类库来测试我的业务逻辑.我在测试类库的根目录中放置了一个 App.config 文件.据我所知,当测试库调用作为业务逻辑库一部分的数据访问过程时,应该访问和使用测试库的 App.config 文件中的连接设置.但是,当我尝试运行我的单元测试时,我收到错误消息,表明测试库的 App.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.

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

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);
  }
}

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

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?

推荐答案

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

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.但是,如果通过将 dll 从 Windows 资源管理器拖到 NUnit GUI 中来将程序集添加到 NUnit 项目,那么它将访问 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.

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

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.

这篇关于从通过单元测试项目调用的类库访问 App.Config 设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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