ASP.NET 系列:单元测试之StructureMap
ASP.NET使用StructureMap等依赖注入组件时最重要就是EntityFramework的DbContext对象要保证在每次HttpRequest只有一个DbContext实例,这里将使用第三方提供的HttpSimulator进行测试。
1.定义IDependency接口
创建屏蔽不同依赖注入组件使用差别的接口。
public interface IDependency
{
void Build(); void EndRequest(); void AddTransient(Type from, Type to, object instance = null); void AddScoped(Type from, Type to, object instance = null); void AddSingleton(Type from, Type to, object instance = null); object GetInstance(Type type); IEnumerable GetAllInstances(Type type);
}
2.提供StructureMap的适配类StructureMapAdapter
public class StructureMapAdapter : IDependency, IDisposable
{
private bool _disposed = false;
private Container _container;
private Registry _registry; public StructureMapAdapter()
{
this._registry = new Registry();
} public void Build()
{
_container = new Container(_registry);
} public void EndRequest()
{
HttpContextLifecycle.DisposeAndClearAll();
} public void AddTransient(Type from, Type to, object instance = null)
{
if (instance == null)
{
_registry.For(from).Use(to).LifecycleIs<TransientLifecycle>();
}
else
{
_registry.For(from).Use(instance).LifecycleIs<TransientLifecycle>();
}
} public void AddScoped(Type from, Type to, object instance = null)
{
if (instance == null)
{
_registry.For(from).Use(to).LifecycleIs<HttpContextLifecycle>();
}
else
{
_registry.For(from).Use(instance).LifecycleIs<HttpContextLifecycle>();
}
} public void AddSingleton(Type from, Type to, object instance = null)
{
if (instance == null)
{
_registry.For(from).Use(to).LifecycleIs<SingletonLifecycle>();
}
else
{
_registry.For(from).Use(instance).LifecycleIs<SingletonLifecycle>();
}
} public object GetInstance(Type type)
{
return _container.GetInstance(type);
} public IEnumerable GetAllInstances(Type type)
{
return _container.GetAllInstances(type);
} public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
} protected virtual void Dispose(bool disposing)
{
if (!_disposed)
{
if (disposing)
{
this._container.Dispose();
} _disposed = true;
}
}
}
3.使用HttpSimulator进行单元测试
public class StructureMapAdapterTest
{
[Fact]
public void TransientTest()
{
IDependency dependency = new StructureMapAdapter();
dependency.AddTransient(typeof(ITest), typeof(Test));
dependency.Build();
var version1 = ((ITest)dependency.GetInstance(typeof(ITest))).Version;
var version2 = ((ITest)dependency.GetInstance(typeof(ITest))).Version;
Assert.NotEqual(version1, version2);
} [Fact]
public void SingletonTest()
{
IDependency dependency = new StructureMapAdapter();
dependency.AddSingleton(typeof(ITest), typeof(Test));
dependency.Build();
var version1 = ((ITest)dependency.GetInstance(typeof(ITest))).Version;
var version2 = ((ITest)dependency.GetInstance(typeof(ITest))).Version;
Assert.Equal(version1, version2);
} [Fact]
public void ScopedTest()
{
var version1 = "";
var version2 = "";
using (HttpSimulator simulator = new HttpSimulator())
{
IDependency dependency = new StructureMapAdapter();
dependency.AddScoped(typeof(ITest), typeof(Test));
dependency.Build();
simulator.SimulateRequest(new Uri("http://localhost/"));
version1 = ((ITest)dependency.GetInstance(typeof(ITest))).Version;
version2 = ((ITest)dependency.GetInstance(typeof(ITest))).Version;
Assert.Equal(version1, version2);
} using (HttpSimulator simulator = new HttpSimulator())
{
IDependency dependency = new StructureMapAdapter();
dependency.AddScoped(typeof(ITest), typeof(Test));
dependency.Build();
simulator.SimulateRequest(new Uri("http://localhost/"));
version1 = ((ITest)dependency.GetInstance(typeof(ITest))).Version;
}
using (HttpSimulator simulator = new HttpSimulator())
{
IDependency dependency = new StructureMapAdapter();
dependency.AddScoped(typeof(ITest), typeof(Test));
dependency.Build();
simulator.SimulateRequest(new Uri("http://localhost/"));
version2 = ((ITest)dependency.GetInstance(typeof(ITest))).Version;
}
Assert.NotEqual(version1, version2);
}
} public interface ITest
{
String Version { get; }
} public class Test : ITest
{
private string _version = Guid.NewGuid().ToString(); public string Version { get { return this._version; } }
}
运行结果:

ASP.NET 系列:单元测试之StructureMap的更多相关文章
- 补习系列(8)-springboot 单元测试之道
目录 目标 一.About 单元测试 二.About Junit 三.SpringBoot-单元测试 项目依赖 测试样例 四.Mock测试 五.最后 目标 了解 单元测试的背景 了解如何 利用 spr ...
- 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单元测试 ...
- 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 ...
- ASP.NET 系列:单元测试
单元测试可以有效的可以在编码.设计.调试到重构等多方面显著提升我们的工作效率和质量.github上可供参考和学习的各种开源项目众多,NopCommerce.Orchard等以及微软的asp.net m ...
- 使用VisualStudio进行单元测试之二
借着工作忙的借口,偷了两天懒,今天继续单元测试之旅.前面说了如何进行一个最简单的单元测试,这次呢就跟大家一起来熟悉一下,在visual studio中如何进行数据驱动的单元测试. 开始之前先来明确一下 ...
- iOS 单元测试之XCTest详解(一)
iOS 单元测试之XCTest详解(一) http://blog.csdn.net/hello_hwc/article/details/46671053 原创blog,转载请注明出处 blog.csd ...
- 玩转单元测试之Testing Spring MVC Controllers
玩转单元测试之 Testing Spring MVC Controllers 转载注明出处:http://www.cnblogs.com/wade-xu/p/4311657.html The Spri ...
- 玩转单元测试之WireMock -- Web服务模拟器
玩转单元测试之WireMock -- Web服务模拟器 WireMock 是一个灵活的库用于 Web 服务测试,和其他测试工具不同的是,WireMock 创建一个实际的 HTTP服务器来运行你的 We ...
- 单元测试之NSNull 检测
本文主要讲 单元测试之NSNull 检测,在现实开发中,我们最烦的往往就是服务端返回的数据中隐藏着NSNull的数据,一般我们的做法是通过[data isKindOfClass:[NSNull cla ...
随机推荐
- 使用 xtrabackup 进行MySQL数据库物理备份
0. xtrabackup的功能 能实现的功能: 非阻塞备份innodb等事务引擎数据库. 备份myisam表会阻塞(需要锁). 支持全备.增量备份.压缩备份. 快速增量备份(xtradb,原理类似于 ...
- itextpd f生成 pdf 文件
一.简介 itextpdf 是一个开源的允许你去创建和操作PDF文档的库.它使的开发者可以提高web和其他应用来动态地生成或操作PDF文档.通过iText 中的Document和PdfWriter类, ...
- C#设计模式(21)——责任链模式
一.引言 在现实生活中,有很多请求并不是一个人说了就算的,例如面试时的工资,低于1万的薪水可能技术经理就可以决定了,但是1万~1万5的薪水可能技术经理就没这个权利批准,可能就需要请求技术总监的批准,所 ...
- 理解linux and inode
inode是一个重要概念,是理解Unix/Linux文件系统和硬盘储存的基础. 我觉得,理解inode,不仅有助于提高系统操作水平,还有助于体会Unix设计哲学,即如何把底层的复杂性抽象成一个简单概念 ...
- redis安装及基础操作(1)
============================================================= 编译安装 0.环境 Linux:centos6.5 redis:3.0.5 ...
- org.eclipse.ui.menus扩展点学习
Eclipse菜单: menu:help?after=addtions menu:navigate?after=open.ext2 menu:window?after=newEditor menu:f ...
- 在报表中给session赋值实现报表间参数共享
1. 问题描述 在报表开发工具FineReport中,若有几张不同的报表,每张报表都有一个共同的项可以选择,比如日期时间.我们希望选择了第一张报表的时间之后,其他报表的默认时间都变为第一张报表选择的时 ...
- NOIP2010提高组乌龟棋 -SilverN
题目背景 小明过生日的时候,爸爸送给他一副乌龟棋当作礼物. 题目描述 乌龟棋的棋盘是一行N个格子,每个格子上一个分数(非负整数).棋盘第1格是唯一的起点,第N格是终点,游戏要求玩家控制一个乌龟棋子从起 ...
- 第二届中国移动互联网测试大会PPT
第二届中国移动互联网测试大会PPT下载_360云盘 (提取密码:7799) 第二届中国移动互联网测试大会PPT下载_百度云盘 (提取密码: ws8m) 第二届中国移动互联网测试大会PPT下载_Goog ...
- [转]mysql 乱码问题解决终结
http://www.th7.cn/db/mysql/2011-07-07/9217.shtml 查看 MYSQL的字符设置,在SQL查询界面输入 SHOW VARIABLES LIKE 'chara ...