问题描述
我有以下设置:
- ASP.net 3.5网站项目
- 具有业务逻辑的类库
- 用于单元测试的C#类库
业务逻辑库所有的db访问。它通过访问System.Configuration.ConfigurationManager.ConnectionStrings从Web站点的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,通过Project> Add装配,它不访问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添加程序集,然后进入NUnit GUI>项目>编辑,并将配置文件名设置为配置文件的名称(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.
这篇关于通过单元测试项目调用的类库中的Access App.Config设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!