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. 1、Hadoop的伪分布式部署

    伪分布式模式搭建:   1.环境准备 (1)主机名(root用户) # vi /etc/sysconfig/network HOSTNAME=hadoo1 (不要用下划线) (2)创建普通用户cong ...

  2. Sql Server 2008R2 遇到了BCP导入各种中文乱码的问题

    今天玩BCP导入数据的时候,有文件格式,有中文字符串问题……以下是历程,和大家分享一下,希望不要走我的弯路 主要那个表是一个翻译表,一个文件里面内涵几十种语言,所以很容易发现问题. 0.使用最常用的语 ...

  3. 如何获得浏览器localStorage的剩余容量

    一.如何获取localStorage的剩余容量 在H5大行其道的今天,localStorage(本地存储)对每一个前断攻城师来说都不太陌生.同时localStorage也给我们带来了极大的便利,不用于 ...

  4. & fg jobs bg

    & 执行程序的后面加&可以将程序转到后台(这个后台是当前会话的后台,并不是守护进程)执行,即$./a.out &,这样我们在打开诸如$gedit test.txt的时候可以写成 ...

  5. lsattr, chattr

    lsattr $lsattr #查看文件的隐藏属性 $lsattr -------------e- ./bookmarks-2016-10-11.json -------------e- ./rxvt ...

  6. Linux工具之man手册彩色页设置

    说明: 对于我们开发人员或者运维工程师来说,经常要查询某个系统命令或者C函数接口的使用方法,最好的最专业的资料就是man手册,通过一些设置可以让man手册页面显示适当颜色,方便阅读,增强美观性. 设置 ...

  7. yum或apt基本源设置指南

    关于: 管理Linux服务器的运维或开发人员经常需要安装软件,最常用方式应该是通过Linux系统提供的包管理工具来在线安装,比如centos的yum,ubuntu或debian的apt-get.当然这 ...

  8. shell 面试题

      1. 用sed修改test.txt的23行test为tset:     sed –i ‘23s/test/tset/g’ test.txt 2. 查看/web.log第25行第三列的内容.     ...

  9. hdu 4967 Handling the Past

    hdu 4967 Handling the Past view code//把时间离散化,维护一个线段(线段l到r的和用sum[l,r]表示),pop的时候就在对应的时间减一,push则相反 //那么 ...

  10. 边工作边刷题:70天一遍leetcode: day 1

    (今日完成:Two Sum, Add Two Numbers, Longest Substring Without Repeating Characters, Median of Two Sorted ...