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 ...
随机推荐
- python中列表、元组、字典内部功能介绍
一.列表(list) 常用功能的介绍:
- coursera机器学习笔记-多元线性回归,normal equation
#对coursera上Andrew Ng老师开的机器学习课程的笔记和心得: #注:此笔记是我自己认为本节课里比较重要.难理解或容易忘记的内容并做了些补充,并非是课堂详细笔记和要点: #标记为<补 ...
- CentOS7安装图形界面和修改运行级别
CentOS7系统如果用mini镜像安装或者服务器版本安装,默认是没有安装图形界面的.如果需要额外去安装图形界面,可以手动来安装CentOS Gnome GUI包.然后会总结一下,在CentOS7系统 ...
- PlaceHolder的两种实现方式
placeholder属性是HTML5 中为input添加的.在input上提供一个占位符,文字形式展示输入字段预期值的提示信息(hint),该字段会在输入为空时显示. 如 <input typ ...
- ELF Format 笔记(一)—— 概述
ilocker:关注 Android 安全(新手) QQ: 2597294287 ELF Object files 参与程序的链接和执行,从这两个角度分别有两种视图: ELF header 位于文件的 ...
- poj 2104 K-th Number(可持久线段树)
K-th Number 持久化:http://www.cnblogs.com/tedzhao/archive/2008/11/12/1332112.html 结构:http://www.docin.c ...
- 深入探讨 java.lang.ref 包
深入探讨 java.lang.ref 包 本文主要探讨了 java.lang.ref 包的使用方法,以及源码解读.并就该包在不同 JVM 上的表现进行了比较与分析.通过阅读本文,读者可以加深对 jav ...
- div根据内容改变大小并且左右居中
div{ display:inline-block; width:auto; } 这个div的父元素text-align:center;
- linux内核启动以及文件系统的加载过程
Linux 内核启动及文件系统加载过程 当u-boot 开始执行 bootcmd 命令,就进入 Linux 内核启动阶段.普通 Linux 内核的启动过程也可以分为两个阶段.本文以项目中使用的 lin ...
- [No000066]python各种类型转换-int,str,char,float,ord,hex,oct等
int(x [,base ]) #将x转换为一个整数 long(x [,base ]) #将x转换为一个长整数 float(x ) #将x转换到一个浮点数 complex(real [,imag ]) ...