Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Changing App.Config on the fly for unit testing

0.00/5 (No votes)
14 Jun 2011CPOL 8.7K  
An alternative is to abstract the settings that you intend to use.So instead of changing your app.config on the fly, simply change a mock or the instance values of the configuration object.For example:public interface IApplicationConfiguration{ string MaybeAFolderINeed { get; } ...

An alternative is to abstract the settings that you intend to use.


So instead of changing your app.config on the fly, simply change a mock or the instance values of the configuration object.


For example:


C#
public interface IApplicationConfiguration
{
    string MaybeAFolderINeed { get; }
    string MyConnectionString { get; }
}

Then you could mock it and return the relevant values.


C#
public class ClassRequiringConfig
{
    public ClassRequiringConfig(IApplicationConfiguration configuration) { ... }
    public ClassRequiringConfig()
           : this(new DefaultApplicationConfigurationImpl()) { ... }
}

Just a thought.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)