65.9K
CodeProject is changing. Read more.
Home

Changing App.Config on the fly for unit testing

emptyStarIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

0/5 (0 vote)

Jun 14, 2011

CPOL
viewsIcon

8708

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:

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

Then you could mock it and return the relevant values.

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

Just a thought.