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 ...
随机推荐
- EF深入系列--Code First
首先是创建DbContext,有两种途径 ①手动编写DbContext代码,同时还要记得去配置文件中添加connectionStrings public class BooksContext : Db ...
- 【Oracle XE系列之一】Windows10_X64环境 安装Oracle XE11gR2 X64数据库
一.安装 1.去Oracle官网下载XE版的安装包[下载路径](Oracle Database Express Edition 11g Release 2 for Windows x64),解压. 2 ...
- spring中各jar功能及jar包之间的依赖关系
(1) spring-core.jar 这个jar文件包含Spring框架基本的核心工具类,Spring其它组件要都要使用到这个包里的类,是其它组件的基本核心,当然你也可以在自己的应用系统中使用这些工 ...
- Tomcat中取消断点
启动tomcat时,myeclipse报错: This kind of launch is configured to openthe debug perspective when it suspen ...
- 烂泥:Linux源码包制作RPM包之Apache
本文由秀依林枫提供友情赞助,首发于烂泥行天下 公司服务器比较多,需要把apache源码包制作成rpm包,然后放到公司内网yum源上进行下载安装.apache的rpm包安装方式比源码安装方式比较快,这能 ...
- bootstrap简单的过一遍
注:.xxxx 表示类(class=xxxx) <h1>到<h6>均可使用.另外还提供了.h1到.h6的class .lead可以让段落突出显示 <small> ...
- android The public type classname must be defined in its own file 报错
The public type classname must be defined in its own file classname 为类名 错误提示,公用的类必髯有自己拥有独立.java文件 解 ...
- android setLayoutParams 问题,出错
LinearLayout layt = (LinearLayout) rootView.findViewById(R.id.llt_2); FrameLayout.LayoutParams layou ...
- 使用HTTP方式远程连接PowerShell
借助Windows PowerShell,我们可以像管理Linux一样使用命令行来远程管理Windows服务器.但是默认情况下,我们只能在域环境中使用PowerShell Remoting.如果是通过 ...
- 失眠害死人-jQuery&AJAX
hi 又是两天没有做事情,后悔什么的只能带来更多的后悔吧,好好做事,忘了两天前自己作出来的失眠 1.jQuery -----jQuery与AJAX-----(PS:ajax是什么请自行百度,或者看我之 ...