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

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

问题描述

我有以下设置:


  • ASP.net 3.5网站项目

  • C#类库与业务逻辑

  • C#类库进行单元测试

业务逻辑库做所有的DB访问。它通过访问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的图形用户界面中调用时?

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?

推荐答案

我刚刚找到了解决办法<一href=\"http://web.archive.org/web/20100619012049/http://www.safnet.com/writing/tech/archives/2007/12/nunit_ignores_a.html\"相对=nofollow>此处。通过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的图形用户界面,并通过项目去添加组件>添加组件,它不会访问在app.config。但是,如果通过拖动从Windows资源管理器的DLL到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添加的组件,然后再在NU​​nit的图形用户界面>项目>编辑,设置配置文件名称的配置文件的名称(VS将设置这name.of.your。 dll.config)和项目基地设到项目的\\ bin \\ Debug目录(这些都是在后台,当你在装配拖VS手动添加它做额外的步骤。

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天全站免登陆