views:

4974

answers:

4

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?

A: 

Try WebConfigurationManager.OpenWebConfiguration() method

Configuration config = WebConfigurationManager.OpenWebConfiguration(@"x:\path\web.config");
            KeyValueConfigurationCollection appSettings = config.AppSettings.Settings;
            string connString = appSettings["connString"].Value;
Jan Remunda
Just tried this. WebConfigurationManager.OpenWebConfiguration needs a virtual path - and since I am not calling this from within the site, I can't provide a virtual path.
Yaakov Ellis
+6  A: 

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.

Yaakov Ellis
+3  A: 

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

azheglov
My situation is that I am introducing unit testing into the project after quite a bit of work is done. I have moved all of the code from App_Code into its own stand-alone library in order to be able to do unit testing. But at this point, dependency injection is a bit out of scope.
Yaakov Ellis
+1  A: 

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

lubos hasko