public BlogServiceIntegrationTestBase() { // ---------------------------------------------------------------- repos var catRepo = new SqlCategoryRepository(_db); var tagRepo = new SqlTagRepository(_db); var postRepo = new SqlPostRepository(_db); // ---------------------------------------------------------------- mock SettingService _settingSvcMock = new Mock <ISettingService>(); _settingSvcMock.Setup(svc => svc.GetSettingsAsync <CoreSettings>()).Returns(Task.FromResult(new CoreSettings())); _settingSvcMock.Setup(svc => svc.GetSettingsAsync <BlogSettings>()).Returns(Task.FromResult(new BlogSettings())); // ---------------------------------------------------------------- mock AppSettings var appSettingsMock = new Mock <IOptionsSnapshot <AppSettings> >(); appSettingsMock.Setup(o => o.Value).Returns(new AppSettings()); // ---------------------------------------------------------------- mock IStorageProvider _storageProviderMock = new Mock <IStorageProvider>(); _storageProviderMock.Setup(pro => pro.StorageEndpoint).Returns(STORAGE_ENDPOINT); // ---------------------------------------------------------------- MediaService var mediaRepo = new SqlMediaRepository(_db); _mediaSvc = new MediaService(_storageProviderMock.Object, appSettingsMock.Object, mediaRepo); // ---------------------------------------------------------------- Cache var serviceProvider = new ServiceCollection().AddMemoryCache().AddLogging().BuildServiceProvider(); var memCacheOptions = serviceProvider.GetService <IOptions <MemoryDistributedCacheOptions> >(); var cache = new MemoryDistributedCache(memCacheOptions); // ---------------------------------------------------------------- LoggerFactory _loggerFactory = serviceProvider.GetService <ILoggerFactory>(); var loggerBlogSvc = _loggerFactory.CreateLogger <BlogPostService>(); var loggerCatSvc = _loggerFactory.CreateLogger <CategoryService>(); var loggerTagSvc = _loggerFactory.CreateLogger <TagService>(); // ---------------------------------------------------------------- Mapper, Shortcode var mapper = BlogUtil.Mapper; var shortcodeSvc = new Mock <IShortcodeService>(); // ---------------------------------------------------------------- MediatR and Services var services = new ServiceCollection(); services.AddScoped <ServiceFactory>(p => p.GetService); // MediatR.ServiceFactory services.AddSingleton <IDistributedCache>(cache); // cache services.AddSingleton <FanDbContext>(_db); // DbContext for repos services.Scan(scan => scan .FromAssembliesOf(typeof(IMediator), typeof(ILogger), typeof(IBlogPostService), typeof(ISettingService)) .AddClasses() .AsImplementedInterfaces()); var provider = services.BuildServiceProvider(); var mediator = provider.GetRequiredService <IMediator>(); _catSvc = new CategoryService(catRepo, _settingSvcMock.Object, mediator, cache, loggerCatSvc); _tagSvc = new TagService(tagRepo, mediator, cache, loggerTagSvc); _imgSvc = new ImageService(_mediaSvc, _storageProviderMock.Object, appSettingsMock.Object); // the blog service _blogSvc = new BlogPostService( _settingSvcMock.Object, postRepo, cache, loggerBlogSvc, mapper, shortcodeSvc.Object, mediator); }
public BlogServiceIntegrationTestBase() { // ---------------------------------------------------------------- repos var catRepo = new SqlCategoryRepository(_db); var tagRepo = new SqlTagRepository(_db); var postRepo = new SqlPostRepository(_db); // ---------------------------------------------------------------- mock SettingService _settingSvcMock = new Mock <ISettingService>(); _settingSvcMock.Setup(svc => svc.GetSettingsAsync <CoreSettings>()).Returns(Task.FromResult(new CoreSettings())); _settingSvcMock.Setup(svc => svc.GetSettingsAsync <BlogSettings>()).Returns(Task.FromResult(new BlogSettings())); // ---------------------------------------------------------------- mock AppSettings var appSettingsMock = new Mock <IOptionsSnapshot <AppSettings> >(); appSettingsMock.Setup(o => o.Value).Returns(new AppSettings()); // ---------------------------------------------------------------- mock IStorageProvider _storageProviderMock = new Mock <IStorageProvider>(); _storageProviderMock.Setup(pro => pro.StorageEndpoint).Returns(STORAGE_ENDPOINT); // ---------------------------------------------------------------- MediaService var mediaRepo = new SqlMediaRepository(_db); _mediaSvc = new MediaService(_storageProviderMock.Object, appSettingsMock.Object, mediaRepo); // ---------------------------------------------------------------- Cache var serviceProvider = new ServiceCollection().AddMemoryCache().AddLogging().BuildServiceProvider(); var memCacheOptions = serviceProvider.GetService <IOptions <MemoryDistributedCacheOptions> >(); var cache = new MemoryDistributedCache(memCacheOptions); // ---------------------------------------------------------------- LoggerFactory _loggerFactory = serviceProvider.GetService <ILoggerFactory>(); var loggerBlogSvc = _loggerFactory.CreateLogger <BlogService>(); // ---------------------------------------------------------------- Mapper, Shortcode, Mediator var mapper = BlogUtil.Mapper; var shortcodeSvc = new Mock <IShortcodeService>(); var mediatorMock = new Mock <IMediator>(); // the blog service _blogSvc = new BlogService( _settingSvcMock.Object, catRepo, postRepo, tagRepo, _mediaSvc, _storageProviderMock.Object, appSettingsMock.Object, cache, loggerBlogSvc, mapper, shortcodeSvc.Object, mediatorMock.Object); }