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

1.定义IConfigurationManager接口

原本依赖ConfigurationManager的代码现在依赖IConfigurationManager。可以在单元测试时方便的Mock。

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

2.创建适配类ConfigurationManagerWrapper

非单元测试环境使用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);
}
}

3.自定义泛型配置接口

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

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

4.实现泛型接口配置接口的.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.对泛型配置接口的实现进行单元测试

使用最流行的单元测试框架和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 = ;
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静态类的代码转换为依赖IConfigurationManager接口,运行时注入ConfigurationManagerWrapper实现类。单元测试时使用Mock模拟IConfigurationManager对象。

ASP.NET 系列:单元测试之ConfigurationManager的更多相关文章

  1. 补习系列(8)-springboot 单元测试之道

    目录 目标 一.About 单元测试 二.About Junit 三.SpringBoot-单元测试 项目依赖 测试样例 四.Mock测试 五.最后 目标 了解 单元测试的背景 了解如何 利用 spr ...

  2. ASP.NET Core搭建多层网站架构【3-xUnit单元测试之简单方法测试】

    2020/01/28, ASP.NET Core 3.1, VS2019, xUnit 2.4.0 摘要:基于ASP.NET Core 3.1 WebApi搭建后端多层网站架构[3-xUnit单元测试 ...

  3. ASP.NET Core搭建多层网站架构【12-xUnit单元测试之集成测试】

    2020/02/01, ASP.NET Core 3.1, VS2019, xunit 2.4.1, Microsoft.AspNetCore.TestHost 3.1.1 摘要:基于ASP.NET ...

  4. ASP.NET 系列:单元测试

    单元测试可以有效的可以在编码.设计.调试到重构等多方面显著提升我们的工作效率和质量.github上可供参考和学习的各种开源项目众多,NopCommerce.Orchard等以及微软的asp.net m ...

  5. [转载]单元测试之道(使用NUnit)

    首先来看下面几个场景你是否熟悉 1.你正在开发一个系统,你不断地编码-编译-调试-编码-编译-调试……终于,你负责的功能模块从上到下全部完成且编译通过!你长出一口气,怀着激动而又忐忑的心情点击界面上的 ...

  6. 单元测试之道(使用NUnit)

    首先来看下面几个场景你是否熟悉 1.你正在开发一个系统,你不断地编码-编译-调试-编码-编译-调试……终于,你负责的功能模块从上到下全部完成且编译通过!你长出一口气,怀着激动而 又忐忑的心情点击界面上 ...

  7. 使用VisualStudio进行单元测试之二

    借着工作忙的借口,偷了两天懒,今天继续单元测试之旅.前面说了如何进行一个最简单的单元测试,这次呢就跟大家一起来熟悉一下,在visual studio中如何进行数据驱动的单元测试. 开始之前先来明确一下 ...

  8. iOS 单元测试之XCTest详解(一)

    iOS 单元测试之XCTest详解(一) http://blog.csdn.net/hello_hwc/article/details/46671053 原创blog,转载请注明出处 blog.csd ...

  9. 玩转单元测试之Testing Spring MVC Controllers

    玩转单元测试之 Testing Spring MVC Controllers 转载注明出处:http://www.cnblogs.com/wade-xu/p/4311657.html The Spri ...

随机推荐

  1. 国内Hadoop应用现状

    Hadoop在国内主要以互联网公司为主,下面主要介绍大规模使用Hadoop或研究Hadoop的公司. 1. 百度 百度在2006年就关注了Hadoop并开始调研和使用,截止2012年,总的集群规模超过 ...

  2. Linux基础命令之cat使用方法大全

    今天在学习部署安装openstack的时候,看到一个关于cat的奇怪用法,可能是本人的才疏学浅没见过这种写法,于是乎查阅资料了一番,并进行了总结,希望也能够帮助有需要的朋友. 以下是我总结的几种常用方 ...

  3. android oncreate获取宽高度

    gridView = (GridView) getView().findViewById(R.id.gridView_musicbook); gridView.getViewTreeObserver( ...

  4. C语言中链表怎么删除结点?

    第一个方法: /*根据姓名删除链表的中的学生记录*/ void deleteByName(struct STUDENT * head) { struct STUDENT *p,*q; ]; if(he ...

  5. STM32时钟理解

    转载自 http://blog.sina.com.cn/s/blog_6ebd49350100q6xw.html STM32时钟理解 一.硬件上的连接问题 如果使用内部RC振荡器而不使用外部晶振,请按 ...

  6. [转] Finding the Best Programmer's Font

    原文 Finding the Best Programmer's Font

  7. HashMap的工作原理深入再深入

    前言 首先再次强调hashcode (==)和equals的真正含义(我记得以前有人会说,equals是判断对象内容,hashcode是判断是否相等之类): equals:是否同一个对象实例.注意,是 ...

  8. TCP聊天工具

    //前台书写 import java.io.OutputStream; import java.net.InetAddress; import java.net.Socket; import java ...

  9. dotNet开发游戏微端

    需求分析 功能要求 当玩家使用不支持 unity webplayer 的浏览器进入游戏时,让玩家通过微端玩游戏. 确保微端的功能和页游戏功能一致. 大体功能就是为unity web game开发微端, ...

  10. Unity 5 WebGL vs Web Player

    起原 Unity5.3中看到Web Player未来将到被取消,根据Unity官方blog中称Unity5.4中将会移除web player. 本文从我知道的知识比较一下webPlayer和WebGL ...