I would like to encapsulate AppConfig therefore I prepared class like this:
static AppConfig()
{
var exePath = Assembly.GetExecutingAssembly().Location;
_config = ConfigurationManager.OpenExeConfiguration(exePath);
}
I did that in order to have Save possibility and can add new settings like this:
public static void SetSettings(string key, object value)
{
if (_config.AppSettings.Settings.AllKeys.Contains(key))
{
_config.AppSettings.Settings[key].Value = value.ToString();
}
else
{
_config.AppSettings.Settings.Add(key, value.ToString());
}
}
public static string GetSettings(string key, string defaultValue = "")
{
if (_config.AppSettings.Settings.AllKeys.Contains(key))
{
return _config.AppSettings.Settings[key].Value;
}
return defaultValue;
}
public static void Save()
{
_config.Save(ConfigurationSaveMode.Modified);
}
Unfortunately using such AppCofing in Tests does not loads it.
would like to know how to load config so it would work both in test project and during application runtime.
The unit test should not be reading from external config files. The values being read out should be dependencies to whatever consumes them, so your test can just provide values you require through the constructor, method, property etc.
If you want to unit test getting and setting of values in the config file, then that is probably not worth testing, as that is framework code.