ASP.NET 5 单元测试中使用依赖注入
相关博文:《ASP.NET 5 使用 TestServer 进行单元测试》
在上一篇博文中,主要说的是,使用 TestServer 对 ASP.NET 5 WebApi 进行单元测试,依赖注入在 WebApi Startup.cs 中完成,所以 UnitTest 中只需要使用 TestServer 启动 WebApi 站点就可以了,因为整个解决方案的对象都是用 ASP.NET 5 “自带”的依赖注入进行管理,所以,在对 ASP.NET 5 类库进行单元测试的时候,我都是手动进行 new 创建的对象,比如针对 Application 的单元测试,贴一段 AdImageServiceTest 中的代码:
namespace CNBlogs.Ad.Application.Tests
{
public class AdTextServiceTest : BaseTest
{
private IAdTextService _adTextService;
public AdTextServiceTest(ITestOutputHelper output)
: base(output)
{
Bootstrapper.Startup.ConfigureMapper();
IUnitOfWork unitOfWork = new UnitOfWork(dbContext);
IMemcached memcached = new EnyimMemcached(null);
_adTextService = new AdTextService(new AdTextRepository(dbContext),
new AdTextCreativeRepository(dbContext),
new UserService(memcached),
unitOfWork,
memcached);
}
[Fact]
public async Task GetByAdTextsTest()
{
var adTexts = await _adTextService.GetAdTexts();
Assert.NotNull(adTexts);
adTexts.ForEach(x => Console.WriteLine($"{x.Title}({x.Link})"));
}
}
}
AdImageServiceTest 构造函数中的代码,是不是看起来很别扭呢?我当时这样写,也是没有办法的,因为依赖注入的配置是写在 Startup 中,比如下面代码:
namespace CNBlogs.Ad.WebApi
{
public class Startup
{
public Startup(IHostingEnvironment env, IApplicationEnvironment appEnv)
{
// Set up configuration sources.
var builder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.AddEnvironmentVariables();
Configuration = builder.Build();
}
public IConfigurationRoot Configuration { get; set; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddMvc();
services.AddSingleton<IUnitOfWork, UnitOfWork>();
services.AddScoped<IDbContext, EFDbContext>();
services.AddSingleton<IAdTextRepository, AdTextRepository>();
services.AddSingleton<IAdTextService, AdTextService>();
}
}
}
这样设计导致的结果是,针对类库项目的单元测试,就没办法使用依赖注入获取对象了,我后来想使用针对 WebApi 单元测试的方式,来对类库进行单元测试,比如用 TestServer 来启动,但类库中没有办法获取所注入的对象,构成函数注入会报错,[FromServices] 属性注入是 MVC Controller 中的东西,并不支持,所以针对类库的单元测试,和 WebApi 的单元测试并不是一样。
IServiceCollection 的程序包是 Microsoft.Extensions.DependencyInjection.Abstractions,我原来以为它和 ASP.NET 5 Web 应用程序相关,其实它们也没啥关系,你可以脱离 ASP.NET 5 Web 应用程序,独立使用它,比如在类库的单元测试中,但如果这样设计使用,我们首先需要做一个工作,把 Startup.cs 中的 ConfigureServices 配置,独立出来,比如放在 CNBlogs.Ad.Bootstrapper 中,这样 Web 应用程序和单元测试项目,都可以使用它,减少代码的重复,比如我们可以进行下面设计:
namespace CNBlogs.Ad.Bootstrapper
{
public static class Startup
{
public static void Configure(this IServiceCollection services, string connectionString)
{
services.AddEntityFramework()
.AddSqlServer()
.AddDbContext<EFDbContext>(options => options.UseSqlServer(connectionString));
services.AddEnyimMemcached();
ConfigureMapper();
services.AddSingleton<IUnitOfWork, UnitOfWork>();
services.AddScoped<IDbContext, EFDbContext>();
services.AddSingleton<IAdTextRepository, AdTextRepository>();
services.AddSingleton<IAdTextService, AdTextService>();
}
public static void ConfigureMapper()
{
Mapper.CreateMap<CNBlogs.Ad.Domain.Entities.AdText, AdTextDTO>();
}
}
}
ASP.NET 5 WebApi 项目中的 Startup.cs 配置会非常简单,只需要下面代码:
public void ConfigureServices(IServiceCollection services)
{
services.Configure(Configuration["data:ConnectionString"]);//add using CNBlogs.Ad.Bootstrapper;
}
好了,做好上面工作后,单元测试中使用依赖注入就非常简单了,为了减少重复代码,我们可以先抽离出一个 BaseTest:
namespace CNBlogs.Ad.BaseTests
{
public class BaseTest
{
protected readonly ITestOutputHelper output;
protected IServiceProvider provider;
public BaseTest(ITestOutputHelper output)
{
var connectionString = "";
var services = new ServiceCollection();
this.output = output;
services.Configure(connectionString);////add using CNBlogs.Ad.Bootstrapper;
provider = services.BuildServiceProvider();
}
}
}
可以看到,我们并没有使用 TestServer,而是手动创建一个 ServiceCollection,它有点类似于我们之前写依赖注入的 Unity Container,不过它们有很大不同,ServiceCollection 的功能更加强大,从 Bootstrapper.Startup 中可以看到,它可以注入 EF、MVC、Memcache 等等服务对象,BuildServiceProvider 的作用就是获取注入的服务对象,我们下面会用到:
namespace CNBlogs.Ad.Repository.Tests
{
public class AdTextServiceTest : BaseTest
{
private IAdTextService _adTextService;
public AdTextServiceTest(ITestOutputHelper output)
: base(output)
{
_adTextService = provider.GetService<IAdTextService>();
}
[Fact]
public async Task GetByAdTextsTest()
{
var adTexts = await _adTextService.GetAdTexts();
Assert.NotNull(adTexts);
adTexts.ForEach(x => Console.WriteLine($"{x.Title}({x.Link})"));
}
}
}
这段代码和一开始的那段代码,形成了鲜明对比,这就是代码设计的魅力所在!!!
ASP.NET 5 单元测试中使用依赖注入的更多相关文章
- 如何在 ASP.Net Web Forms 中使用依赖注入
依赖注入技术就是将一个对象注入到一个需要它的对象中,同时它也是控制反转的一种实现,显而易见,这样可以实现对象之间的解耦并且更方便测试和维护,依赖注入的原则早已经指出了,应用程序的高层模块不依赖于低层模 ...
- ASP.NET Web API中的依赖注入
什么是依赖注入 依赖,就是一个对象需要的另一个对象,比如说,这是我们通常定义的一个用来处理数据访问的存储,让我们用一个例子来解释,首先,定义一个领域模型如下: namespace Pattern.DI ...
- ASP.NET Core 过滤器中使用依赖注入
如何给过滤器ActionFilterAttribute也用上构造函数注入呢? 一般自定义的过滤器直接用特性方式标识就能使用 [ContentFilter] 因为构造函数在使用的时候要求传参,然后我们可 ...
- ASP.NET Core 中的 依赖注入介绍
ASP.NET Core 依赖注入 HomeController public class HomeController : Controller { private IStudentReposito ...
- ASP.NET Core 中文文档 第四章 MVC(3.8)视图中的依赖注入
原文:Dependency injection into views 作者:Steve Smith 翻译:姚阿勇(Dr.Yao) 校对:孟帅洋(书缘) ASP.NET Core 支持在视图中使用 依赖 ...
- ASP.NET Core 在 JSON 文件中配置依赖注入
前言 在上一篇文章中写了如何在MVC中配置全局路由前缀,今天给大家介绍一下如何在在 json 文件中配置依赖注入. 在以前的 ASP.NET 4+ (MVC,Web Api,Owin,SingalR等 ...
- ASP.NET Core中的依赖注入(1):控制反转(IoC)
ASP.NET Core在启动以及后续针对每个请求的处理过程中的各个环节都需要相应的组件提供相应的服务,为了方便对这些组件进行定制,ASP.NET通过定义接口的方式对它们进行了"标准化&qu ...
- ASP.NET Core中的依赖注入(2):依赖注入(DI)
IoC主要体现了这样一种设计思想:通过将一组通用流程的控制从应用转移到框架之中以实现对流程的复用,同时采用"好莱坞原则"是应用程序以被动的方式实现对流程的定制.我们可以采用若干设计 ...
- ASP.NET Core中的依赖注入(3): 服务的注册与提供
在采用了依赖注入的应用中,我们总是直接利用DI容器直接获取所需的服务实例,换句话说,DI容器起到了一个服务提供者的角色,它能够根据我们提供的服务描述信息提供一个可用的服务对象.ASP.NET Core ...
随机推荐
- HashMap实现缓存
package com.cache; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import org.a ...
- browsersync实现网页实时刷新(修改LESS,JS,HTML时)
var gulp = require("gulp"), less = require("gulp-less"), browserSync = require(& ...
- Linux入门之路
一.linux简介 Linux前身:Minix,由Andrew S. Tanenbaum教授参考Unix编写 Linux创始人:Linus Torvalds Linux内核版(只有内核)和发行版(在内 ...
- bzoj2599: [IOI2011]Race(点分治)
写了四五道点分治的题目了,算是比较理解点分治是什么东西了吧= = 点分治主要用来解决点对之间的问题的,比如距离为不大于K的点有多少对. 这道题要求距离等于K的点对中连接两点的最小边数. 那么其实道理是 ...
- JSP实现在项目在网页上查询
<table> <caption>user</caption> <tr> <td>编号</td> <td>姓名< ...
- Jquery 系列(1) 基本认识
本系列Jquery所用测试Demo版本是<uncompressed,development jQuery 1.11.3> 最新的jquery包可以从官网下载请参照http://jquery ...
- PHP的日期和时间处理函数
1. 将日期和时间转变为时间戳 1.1 time() 原型:time(void) 作用:返回当前时间的 UNIX时间戳. 参数:void,可选(即无参数) 1.2 mktime() 原型:int mk ...
- Box-sizing:小身材,大拳头!
国庆回来,很久没写博客了.一来是自己毫无时间,二是最近开发任务特别紧,三是节后综合症,脑子一片空白没有找到写作的原材料.今天,在加完班回来的22点,忙里偷闲,分享一下最近学到的一个小知识点如题.标题的 ...
- Guava Supplier实例
今天想讲一下Guava Suppliers的几点用法.Guava Suppliers的主要功能是创建包裹的单例对象,通过get方法可以获取对象的值.每次获取的对象都为同一个对象,但你和单例模式有所区别 ...
- Nodejs之MEAN栈开发(三)---- 使用Mongoose创建模型及API
继续开扒我们的MEAN栈开发之路,前面两节我们学习了Express.Jade引擎并创建了几个静态页面,最后通过Heroku部署了应用. Nodejs之MEAN栈开发(一)---- 路由与控制器 Nod ...