I always wrap the configuration logic so that I can replace (mock) the configuration while the system is under test.
In other words, I wrap my configuration like so:
public interface IConfigurationSettings
{
string SmtpHost { get; }
}
public class ConfigurationSettings : IConfigurationSettings
{
public string SmtpHost
{
get { return ConfigurationManager.AppSettings["SmtpHost"]; }
}
}
public static class Settings
{
private static readonly IConfigurationSettings _configurationSettings;
static Settings()
{
_configurationSettings = IoCHelper.Resolve<IConfigurationSettings>();
}
public static string SmtpHost
{
get { return _configurationSettings.SmtpHost; }
}
}
Then, when the system is under test, I can just mock out my configuration like so:
var settings = new Mock<IConfigurationSettings>();
settings.SetupGet(s => s.SmtpHost).Returns("127.0.0.1");
Resolver.Setup(r => r.Resolve<IConfigurationSettings>()).Returns(settings.Object);