ASP.NET系列:单元测试之ConfigurationManager

王朝学院·作者佚名  2016-08-28
窄屏简体版  字體: 小  |  中  |  大  |  超大  

通过ConfigurationManager使用.NET配置文件时,可以通过添加配置文件进行单元测试,虽然可以通过测试但达不到解耦的目的。使用IConfigurationManager和ConfigurationManagerWrapper对ConfigurationManager进行适配是更好的方式,ConfigurationManagerWrapper提供.NET配置文件方式的实现,如果需要支持其他配置,创建IConfigurationManager接口的不同的实现类即可。

1.定义IConfigurationManager接口原本依赖ConfigurationManager的代码现在依赖IConfigurationManager。可以在单元测试时方便的Mock。

publicinterfaceIConfigurationManager

{

NameValueCollection AppSettings {get; }

ConnectionStringSettingsCollection ConnectionStrings {get; }objectGetSection(stringsectionName);

}

2.创建适配类ConfigurationManagerWrapper非单元测试环境使用ConfigurationManagerWrapper作为IConfigurationManager的默认实现。

publicclassConfigurationManagerWrapper : IConfigurationManager

{publicNameValueCollection AppSettings

{get{returnConfigurationManager.AppSettings;

}

}publicConnectionStringSettingsCollection ConnectionStrings

{get{returnConfigurationManager.ConnectionStrings;

}

}publicobjectGetSection(stringsectionName)

{returnConfigurationManager.GetSection(sectionName);

}

}

3.自定义泛型配置接口在我们的代码需要使用配置时,可以考虑创建通用的泛型接口也可以使用专用的强类型的接口。这里演示使用通用的接口。

publicinterfaceIConfiguration

{

T Get<T>(stringkey, T @default);

}

4.实现泛型接口配置接口的.NET配置文件版本AppConfigAdapter直接不使用ConfigurationManager而是依赖IConfigurationManager接口。

publicclassAppConfigAdapter : IConfiguration

{PRivateIConfigurationManager _configurationManager;publicAppConfigAdapter(IConfigurationManager configurationManager)

{this._configurationManager =configurationManager;

}publicT Get<T>(stringnodeName, T @default)

{varvalue =this._configurationManager.AppSettings[nodeName];returnvalue ==null? @default : (T)Convert.ChangeType(value,typeof(T));

}

}

5.对泛型配置接口的实现进行单元测试使用最流行的单元测试框架和Mock类库:xUnit+Moq进行单元测试。

publicclassAppConfigAdapterTest

{

[Fact]publicvoidGetStringTest()

{varkey ="key";varvalue ="value";varconfiguration =newAppConfigAdapter(this.GetConfigurationManager(o =>o.Add(key, value.ToString())));

Assert.Equal(configuration.Get(key,string.Empty), value);

}

[Fact]publicvoidGetIntTest()

{varkey ="key";varvalue =1;varconfiguration =newAppConfigAdapter(this.GetConfigurationManager(o =>o.Add(key, value.ToString())));

Assert.Equal(configuration.Get(key,int.MinValue), value);

}

[Fact]publicvoidGetBoolTest()

{varkey ="key";varvalue =true;varconfiguration =newAppConfigAdapter(this.GetConfigurationManager(o =>o.Add(key, value.ToString())));

Assert.Equal(configuration.Get(key,false), value);

}

[Fact]publicvoidGetDateTimeTest()

{varkey ="key";varvalue =DateTime.Parse(DateTime.Now.ToString());varconfiguration =newAppConfigAdapter(this.GetConfigurationManager(o =>o.Add(key, value.ToString())));

Assert.Equal(configuration.Get(key, DateTime.MinValue), value);

}

[Fact]publicvoidGetDecimalTest()

{varkey ="key";varvalue =1.1m;varconfiguration =newAppConfigAdapter(this.GetConfigurationManager(o =>o.Add(key, value.ToString())));

Assert.Equal(configuration.Get(key,decimal.MinValue), value);

}privateIConfigurationManager GetConfigurationManager(Action<NameValueCollection>set)

{varappSettings =newNameValueCollection();set(appSettings);varconfigurationManager =newMock<IConfigurationManager>();

configurationManager.Setup(o=>o.AppSettings).Returns(appSettings);returnconfigurationManager.Object;

}

}

运行结果:

[flash=500,400]http://s1.knowsky.com/20160211/nynhspajl0g29.png[/flash]

6.总结使依赖ConfigurationManager静态类的代码转换为依赖IConfigurationManager接口,运行时注入ConfigurationManagerWrapper实现类。单元测试时使用Mock模拟IConfigurationManager对象。

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航