通過ConfigurationManager使用.NET配置文件時,可以通過添加配置文件進行單元測試,雖然可以通過測試但達不到解耦的目的。使用IConfigurationManager和ConfigurationManagerWrapper對ConfigurationManager進行適配是更好的方式,ConfigurationManagerWrapper提供.NET配置文件方式的實現,如果需要支持其他配置,創建IConfigurationManager接口的不同的實現類即可。
原本依賴ConfigurationManager的代碼現在依賴IConfigurationManager??梢栽趩卧獪y試時方便的Mock。
public interface IConfigurationManager{ NameValueCollection AppSettings { get; } ConnectionStringSettingsCollection ConnectionStrings { get; } object GetSection(string sectionName);}
非單元測試環境使用ConfigurationManagerWrapper作為IConfigurationManager的默認實現。
public class ConfigurationManagerWrapper : IConfigurationManager{ public NameValueCollection AppSettings { get { return ConfigurationManager.AppSettings; } } public ConnectionStringSettingsCollection ConnectionStrings { get { return ConfigurationManager.ConnectionStrings; } } public object GetSection(string sectionName) { return ConfigurationManager.GetSection(sectionName); }}
在我們的代碼需要使用配置時,可以考慮創建通用的泛型接口也可以使用專用的強類型的接口。這里演示使用通用的接口。
public interface IConfiguration{ T Get<T>(string key, T @default);}
AppConfigAdapter直接不使用ConfigurationManager而是依賴IConfigurationManager接口。
public class AppConfigAdapter : IConfiguration{ PRivate IConfigurationManager _configurationManager; public AppConfigAdapter(IConfigurationManager configurationManager) { this._configurationManager = configurationManager; } public T Get<T>(string nodeName, T @default) { var value = this._configurationManager.AppSettings[nodeName]; return value == null ? @default : (T)Convert.ChangeType(value, typeof(T)); }}
使用最流行的單元測試框架和Mock類庫:xUnit+Moq進行單元測試。
public class AppConfigAdapterTest{ [Fact] public void GetStringTest() { var key = "key"; var value = "value"; var configuration = new AppConfigAdapter(this.GetConfigurationManager(o => o.Add(key, value.ToString()))); Assert.Equal(configuration.Get(key, string.Empty), value); } [Fact] public void GetIntTest() { var key = "key"; var value = 1; var configuration = new AppConfigAdapter(this.GetConfigurationManager(o => o.Add(key, value.ToString()))); Assert.Equal(configuration.Get(key, int.MinValue), value); } [Fact] public void GetBoolTest() { var key = "key"; var value = true; var configuration = new AppConfigAdapter(this.GetConfigurationManager(o => o.Add(key, value.ToString()))); Assert.Equal(configuration.Get(key, false), value); } [Fact] public void GetDateTimeTest() { var key = "key"; var value = DateTime.Parse(DateTime.Now.ToString()); var configuration = new AppConfigAdapter(this.GetConfigurationManager(o => o.Add(key, value.ToString()))); Assert.Equal(configuration.Get(key, DateTime.MinValue), value); } [Fact] public void GetDecimalTest() { var key = "key"; var value = 1.1m; var configuration = new AppConfigAdapter(this.GetConfigurationManager(o => o.Add(key, value.ToString()))); Assert.Equal(configuration.Get(key, decimal.MinValue), value); } private IConfigurationManager GetConfigurationManager(Action<NameValueCollection> set) { var appSettings = new NameValueCollection(); set(appSettings); var configurationManager = new Mock<IConfigurationManager>(); configurationManager.Setup(o => o.AppSettings).Returns(appSettings); return configurationManager.Object; }}
運行結果:
使依賴ConfigurationManager靜態類的代碼轉換為依賴IConfigurationManager接口,運行時注入ConfigurationManagerWrapper實現類。單元測試時使用Mock模擬IConfigurationManager對象。
新聞熱點
疑難解答