Derik Whittaker

Syndication

News


Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
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,


Posted 03-25-2008 8:43 AM by Derik Whittaker
Filed under: , ,

[Advertisement]

Comments

Derick Bailey wrote re: App.Config issues when running NUnit via Resharper
on 03-25-2008 12:41 PM

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.

Derik Whittaker wrote re: App.Config issues when running NUnit via Resharper
on 03-25-2008 12:45 PM

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

Ilya Ryzhenkov wrote re: App.Config issues when running NUnit via Resharper
on 03-25-2008 2:26 PM

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

Derik Whittaker wrote re: App.Config issues when running NUnit via Resharper
on 03-25-2008 2:52 PM

@llya,

Thanks,

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

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)