I was spinning my wheels now, trying to figure out how I can unit test use the following code. At first I was going to use Moq to mock everything, but it does not include the ability to mock private classes. I know that I need to abstract from implementation calls (Configuration) using an interface? but I cannot get everything to work correctly.
The code can be modified, but I would prefer to keep the methods static, unless you can provide good reasons not to. You can add interfaces or create any seams. Alternatively, GetConnStringByName () can be reorganized to return the corresponding string instead of ConnectionStringSettings.
Thoughts?
namespace Stackoverflow.Rocks
{
public class WebConfigStrings
{
public static ConnectionStringSettings GetConnStringByName(string connectionStringName)
{
Configuration rootWebConfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);
ConnectionStringSettings connString;
connString = rootWebConfig.ConnectionStrings.ConnectionStrings[connectionStringName];
return connString;
}
public static string GetAppStringByName(string applicationStringName)
{
string appString = "";
appString = ConfigurationManager.AppSettings[applicationStringName];
return appString;
}
}
}