相關(guān)博文:《asp.net 5 使用 TestServer 進(jìn)行單元測(cè)試》
在上一篇博文中,主要說的是,使用 TestServer 對(duì) ASP.NET 5 WebApi 進(jìn)行單元測(cè)試,依賴注入在 WebApi Startup.cs 中完成,所以 UnitTest 中只需要使用 TestServer 啟動(dòng) WebApi 站點(diǎn)就可以了,因?yàn)檎麄€(gè)解決方案的對(duì)象都是用 ASP.NET 5 “自帶”的依賴注入進(jìn)行管理,所以,在對(duì) ASP.NET 5 類庫進(jìn)行單元測(cè)試的時(shí)候,我都是手動(dòng)進(jìn)行 new 創(chuàng)建的對(duì)象,比如針對(duì) application 的單元測(cè)試,貼一段 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 構(gòu)造函數(shù)中的代碼,是不是看起來很別扭呢?我當(dāng)時(shí)這樣寫,也是沒有辦法的,因?yàn)橐蕾囎⑷氲呐渲檬菍懺?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>(); } }}
這樣設(shè)計(jì)導(dǎo)致的結(jié)果是,針對(duì)類庫項(xiàng)目的單元測(cè)試,就沒辦法使用依賴注入獲取對(duì)象了,我后來想使用針對(duì) WebApi 單元測(cè)試的方式,來對(duì)類庫進(jìn)行單元測(cè)試,比如用 TestServer 來啟動(dòng),但類庫中沒有辦法獲取所注入的對(duì)象,構(gòu)成函數(shù)注入會(huì)報(bào)錯(cuò),[FromServices]
屬性注入是 MVC Controller 中的東西,并不支持,所以針對(duì)類庫的單元測(cè)試,和 WebApi 的單元測(cè)試并不是一樣。
IServiceCollection 的程序包是 Microsoft.Extensions.DependencyInjection.Abstractions
,我原來以為它和 ASP.NET 5 Web 應(yīng)用程序相關(guān),其實(shí)它們也沒啥關(guān)系,你可以脫離 ASP.NET 5 Web 應(yīng)用程序,獨(dú)立使用它,比如在類庫的單元測(cè)試中,但如果這樣設(shè)計(jì)使用,我們首先需要做一個(gè)工作,把 Startup.cs 中的 ConfigureServices 配置,獨(dú)立出來,比如放在 CNBlogs.Ad.Bootstrapper
中,這樣 Web 應(yīng)用程序和單元測(cè)試項(xiàng)目,都可以使用它,減少代碼的重復(fù),比如我們可以進(jìn)行下面設(shè)計(jì):
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 項(xiàng)目中的 Startup.cs 配置會(huì)非常簡(jiǎn)單,只需要下面代碼:
public void ConfigureServices(IServiceCollection services){ services.Configure(Configuration["data:ConnectionString"]);//add using CNBlogs.Ad.Bootstrapper;}
好了,做好上面工作后,單元測(cè)試中使用依賴注入就非常簡(jiǎn)單了,為了減少重復(fù)代碼,我們可以先抽離出一個(gè) 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,而是手動(dòng)創(chuàng)建一個(gè) ServiceCollection,它有點(diǎn)類似于我們之前寫依賴注入的 Unity Container,不過它們有很大不同,ServiceCollection 的功能更加強(qiáng)大,從 Bootstrapper.Startup 中可以看到,它可以注入 EF、MVC、Memcache 等等服務(wù)對(duì)象,BuildServiceProvider 的作用就是獲取注入的服務(wù)對(duì)象,我們下面會(huì)用到:
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})")); } }}
這段代碼和一開始的那段代碼,形成了鮮明對(duì)比,這就是代碼設(shè)計(jì)的魅力所在!!!
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注