Devlico.Us
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @devlicious

Derik Whittaker

Thoughts on Software Development, .Net, OOP, Design Patterns and all things cool



App.Config issues when running NUnit via Resharper

***** Update *****
This issue is directly related to a #R 4 EAP issue.  I was using build 755 and this was an issue.  As of build 762, this is no longer an issue.
********************
 

Today I encountered some odd behavior when trying to run some tests against my configuration object.  I am hoping that someone out there has some insight for me.

My issue

I have a project that produces an assembly by the name of AccessKey.Domain.Tests.dll.  Because I have a app.config file in the project, the config file is renamed to AccessKey.Domain.Tests.dll.config when the project is compiled.

The problem is that when I try to access the appSettings value in the config file I am only getting nulls.  For what ever reason, the .net framework thinks my file should be named AccessKey.Domain.Tests.config (notice the .dll is missing from the name).

I have verified that the framework is in fact trying to pick up the wrong assembly by checking 2 things.

  1. System.AppDomain.CurrentDomain.SetupInformation.ConfigurationFile
  2. ConfigurationManager.OpenExeConfiguration( ConfigurationUserLevel.None ).FilePath

Both of these have point to using the wrong config file name.  I even tried to set the value in the System.AppDomain.CurrentDomain.SetupInformation.ConfigurationFile property, but that did not work.

My Resolution (but is lame)

In order to get this to work, I simply created a config file in my test project named AccessKey.Domain.Tests.config (again, notice the .dll is missing) and set the file to be copied to the output directory at compile time.

I don't like doing this as it feels dirty as well as it is not testing my system correctly.

 

Does anyone have a solution for my problem.  In my 2+ years of testing, I have NEVER run into this issue....... HELP!

Till next time,



Comments

Derick Bailey said:

my answer is that you should never rely on an external config file for your unit tests. Any configuration that you need to do should be done through the use of interfaces and a config provider so that you can mock the configuration in your unit tests and provide resources from the actual config file in the production app.

an extremely simple implementation might look like this:

public interface IConfiguration

{

  public string SomeConfigItem{ get; }

}

public class ConfigurationProvider

{

  IConfiguration _config;

  public static void SetConfiguration(IConfiguration config)

  {

    _config = config;

  }

 public static IConfiguration Current

 {

   get {

     if (_config == null) throw new Exception("No configuration available");

     return _config;

   }

 }  

}

...

then you can provide an implementation of IConfiguraiton that reads from your config files, for the actual application; and a mock implementation for your unit tests.

# March 25, 2008 12:41 PM

Derik Whittaker said:

@Derick,

I 100% agree.  and in fact i did not see this issue for the first 2 days because of this.

However, these tests are actually testing that my config values are valid.  That the values are within valid ranges, etc.

In this case I MUST hit the actual config file.

# March 25, 2008 12:45 PM

Ilya Ryzhenkov said:

It was a known issue in some of the ReSharper 4 nightly builds. It is fixed now, so you may grab latest builds.

# March 25, 2008 2:26 PM

Derik Whittaker said:

@llya,

Thanks,

Did not even think about checking to see if it was a #R issue.

# March 25, 2008 2:52 PM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add

About Derik Whittaker

Derik is a .Net Developer/Architect specializing in WinForms working out the northern suburbs of Chicago. He is also believer and advocate for Agile development including SCRUM, TDD, CI, etc.

When Derik is not writing code he can be found spending time with his wife and young son, climbing on his bouldering wall, watching sports (mostly baseball), and generally vegging out. Check out Devlicio.us!

Our Sponsors

Proudly Partnered With


This Blog

Syndication

News