国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

ASP.NET5單元測試中使用依賴注入

2019-11-14 14:25:09
字體:
來源:轉載
供稿:網友

相關博文:《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})"));        }    }}

這段代碼和一開始的那段代碼,形成了鮮明對比,這就是代碼設計的魅力所在!!!


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 罗山县| 泾源县| 彰化市| 珠海市| 陵水| 南宁市| 海淀区| 麻栗坡县| 松滋市| 阿拉尔市| 开阳县| 深水埗区| 宁波市| 德清县| 岢岚县| 朔州市| 右玉县| 韩城市| 修文县| 鄂托克旗| 高淳县| 文成县| 揭阳市| 宿迁市| 常德市| 虎林市| 石渠县| 秦皇岛市| 德庆县| 遵化市| 汾阳市| 靖远县| 平果县| 鹿泉市| 三原县| 绥化市| 建湖县| 自贡市| 巴彦县| 河南省| 乡城县|