国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

ASP.NET系列:單元測試之ConfigurationManager

2019-11-14 14:27:24
字體:
來源:轉載
供稿:網友

通過ConfigurationManager使用.NET配置文件時,可以通過添加配置文件進行單元測試,雖然可以通過測試但達不到解耦的目的。使用IConfigurationManager和ConfigurationManagerWrapper對ConfigurationManager進行適配是更好的方式,ConfigurationManagerWrapper提供.NET配置文件方式的實現(xiàn),如果需要支持其他配置,創(chuàng)建IConfigurationManager接口的不同的實現(xiàn)類即可。

1.定義IConfigurationManager接口

原本依賴ConfigurationManager的代碼現(xiàn)在依賴IConfigurationManager??梢栽趩卧獪y試時方便的Mock。

public interface IConfigurationManager{    NameValueCollection AppSettings { get; }    ConnectionStringSettingsCollection ConnectionStrings { get; }    object GetSection(string sectionName);}

2.創(chuàng)建適配類ConfigurationManagerWrapper

非單元測試環(huán)境使用ConfigurationManagerWrapper作為IConfigurationManager的默認實現(xiàn)。

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);    }}

3.自定義泛型配置接口

在我們的代碼需要使用配置時,可以考慮創(chuàng)建通用的泛型接口也可以使用專用的強類型的接口。這里演示使用通用的接口。

public interface IConfiguration{    T Get<T>(string key, T @default);}

4.實現(xiàn)泛型接口配置接口的.NET配置文件版本

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));    }}

5.對泛型配置接口的實現(xiàn)進行單元測試

使用最流行的單元測試框架和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;    }}

運行結果:

6.總結

使依賴ConfigurationManager靜態(tài)類的代碼轉換為依賴IConfigurationManager接口,運行時注入ConfigurationManagerWrapper實現(xiàn)類。單元測試時使用Mock模擬IConfigurationManager對象。


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 五华县| 福海县| 秭归县| 剑阁县| 嘉义市| 夹江县| 富宁县| 闸北区| 九龙城区| 霸州市| 赤城县| 西充县| 鄂尔多斯市| 新田县| 始兴县| 永胜县| 朝阳县| 涡阳县| 利津县| 广汉市| 赤壁市| 海伦市| 沧源| 二手房| 如皋市| 花莲县| 汨罗市| 南郑县| 金门县| 麻栗坡县| 弥渡县| 枣强县| 鄂温| 云和县| 沂源县| 伊宁市| 九寨沟县| 巴青县| 禹城市| 论坛| 宝清县|