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的更多相关文章

  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. 使用VisualStudio进行单元测试之二

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

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

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

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

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

  8. 玩转单元测试之WireMock -- Web服务模拟器

    玩转单元测试之WireMock -- Web服务模拟器 WireMock 是一个灵活的库用于 Web 服务测试,和其他测试工具不同的是,WireMock 创建一个实际的 HTTP服务器来运行你的 We ...

  9. 单元测试之NSNull 检测

    本文主要讲 单元测试之NSNull 检测,在现实开发中,我们最烦的往往就是服务端返回的数据中隐藏着NSNull的数据,一般我们的做法是通过[data isKindOfClass:[NSNull cla ...

随机推荐

  1. EF深入系列--细节

    1.在调试的时候,查看EF生成的SQL语句 在Context类的构造函数中添加以下代码,就可以在调试的时候在[输出]窗口中看到SQL语句 this.Database.Log = s => Sys ...

  2. 批处理脚本为Mysql重置root密码(重置密码为123456)

    @echo off title mysql ::从注册表找到Mysql的安装路径写入文件mysql.txt reg query HKLM\SYSTEM\ControlSet001\Services\M ...

  3. spark mllib配置pom.xml错误 Multiple markers at this line Could not transfer artifact net.sf.opencsv:opencsv:jar:2.3 from/to central (https://repo.maven.apache.org/maven2): repo.maven.apache.org

    刚刚spark mllib,在maven repository网站http://mvnrepository.com/中查询mllib后得到相关库的最新dependence为: <dependen ...

  4. 警惕多iframe下的同名id引起的诡异问题

    遇到个诡异bug,虽然bug中套bug,忽略次要bug,其中最诡异最典型的现象是多行window.top.$("#id")取值操作,其中有一行却取不到值.这个着实让我费解.因为用到 ...

  5. [转] OpenStack Kilo 更新日志

    OpenStack 2015.1.0 (Kilo)更新日志 原文: https://wiki.openstack.org/wiki/ReleaseNotes/Kilo/zh-hans 目录  [隐藏] ...

  6. [转]C#网络编程(异步传输字符串) - Part.3

    本文转自:http://www.tracefact.net/CSharp-Programming/Network-Programming-Part3.aspx 这篇文章我们将前进一大步,使用异步的方式 ...

  7. c++学习之容器细枝末节(1)

    对照着c++primier 开始学习第九章容器,把课后习题当做练习,虽然是看过书上的讲解,但是做题编程的时候,一些需要注意的地方还是难免有遗漏. 一下是几点印象比较深刻的总结: (1)前几章只学了ve ...

  8. 贪心法 codevs 1052 地鼠游戏

    1052 地鼠游戏  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题解       题目描述 Description 王钢是一名学习成绩优异的学生,在平 ...

  9. 2014 UESTC暑前集训动态规划专题解题报告

    A.爱管闲事 http://www.cnblogs.com/whatbeg/p/3762733.html B.轻音乐同好会 C.温泉旅馆 http://www.cnblogs.com/whatbeg/ ...

  10. flex sdk中mx_internal function getTextField() 这种函数如何调用?

    在用flex 开发中,一些函数前打上了 mx_internal 外部调用不了,其实这样写就可以了 xxx.mx_internal::getTextField() 而 xxx.getTextField( ...