Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have the following setup:

  • ASP.net 3.5 Web Site Project
  • C# Class Library with business logic
  • C# Class Library for unit testing

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.

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

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?

like image 349
Yaakov Ellis Avatar asked Aug 23 '09 12:08

Yaakov Ellis


3 Answers

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

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.

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.

like image 99
Yaakov Ellis Avatar answered Oct 15 '22 09:10

Yaakov Ellis


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.)

like image 45
azheglov Avatar answered Oct 15 '22 09:10

azheglov


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

like image 3
lubos hasko Avatar answered Oct 15 '22 09:10

lubos hasko