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站点的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中调用时,我需要做什么才能使业务逻辑类库成功检索单元测试类库设置?
4 个解决方案
#1
7
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并通过Project> Add Assembly添加程序集,则它不会访问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>项目>编辑,并将配置文件名设置为配置文件的名称(VS将其设置为name.of.your.dll.config )并将Project Base设置为项目的\ bin \ Debug目录(这些是在程序集中拖动并在手动添加它时在后台完成的额外步骤。
#2
6
I'd recommend changing the design such that your business-logic layer, instead of having the responsibility to locate configuration settings, is injected with 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文件中读取的设置,而您的单元测试可能会注入不同的设置(例如,连接字符串到测试数据库等)
#3
3
Just rename app.config
to name.of.your.dll.config
. It works for me.
只需将app.config重命名为name.of.your.dll.config即可。这个对我有用。
#4
0
Try WebConfigurationManager.OpenWebConfiguration() method
尝试使用WebConfigurationManager.OpenWebConfiguration()方法
Configuration config = WebConfigurationManager.OpenWebConfiguration(@"x:\path\web.config");
KeyValueConfigurationCollection appSettings = config.AppSettings.Settings;
string connString = appSettings["connString"].Value;
#1
7
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并通过Project> Add Assembly添加程序集,则它不会访问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>项目>编辑,并将配置文件名设置为配置文件的名称(VS将其设置为name.of.your.dll.config )并将Project Base设置为项目的\ bin \ Debug目录(这些是在程序集中拖动并在手动添加它时在后台完成的额外步骤。
#2
6
I'd recommend changing the design such that your business-logic layer, instead of having the responsibility to locate configuration settings, is injected with 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文件中读取的设置,而您的单元测试可能会注入不同的设置(例如,连接字符串到测试数据库等)
#3
3
Just rename app.config
to name.of.your.dll.config
. It works for me.
只需将app.config重命名为name.of.your.dll.config即可。这个对我有用。
#4
0
Try WebConfigurationManager.OpenWebConfiguration() method
尝试使用WebConfigurationManager.OpenWebConfiguration()方法
Configuration config = WebConfigurationManager.OpenWebConfiguration(@"x:\path\web.config");
KeyValueConfigurationCollection appSettings = config.AppSettings.Settings;
string connString = appSettings["connString"].Value;