/
Airat
/
ASP.NET
Обзор
Документация
Войти
/
Airat
/
ASP.NET
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
NoSQL/src/Pcf.GivingToCustomer/Pcf.GivingToCustomer.IntegrationTests/TestWebApplicationFactory.cs
58 строк
2 KB
Айрат Измаилов
new file: ../../Docker
23 ноя 2024, 20:03
23 ноя 2024, 20:03
d822795
Код
Авторство
О чём код?
using System; using System.Linq; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc.Testing; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Pcf.GivingToCustomer.Core.Abstractions.Gateways; using Pcf.GivingToCustomer.DataAccess; using Pcf.GivingToCustomer.Integration; using Pcf.GivingToCustomer.IntegrationTests.Data; namespace Pcf.GivingToCustomer.IntegrationTests { public class TestWebApplicationFactory<TStartup> : WebApplicationFactory<TStartup> where TStartup: class { protected override void ConfigureWebHost(IWebHostBuilder builder) { builder.ConfigureServices(services => { var descriptor = services.SingleOrDefault( d => d.ServiceType == typeof(DbContextOptions<Удалить_DataContext>)); services.Remove(descriptor); services.AddScoped<INotificationGateway, NotificationGateway>(); services.AddDbContext<Удалить_DataContext>(x => { x.UseSqlite("Filename=PromoCodeFactoryDb.sqlite"); //x.UseNpgsql(Configuration.GetConnectionString("PromoCodeFactoryDb")); x.UseSnakeCaseNamingConvention(); x.UseLazyLoadingProxies(); }); var sp = services.BuildServiceProvider(); using var scope = sp.CreateScope(); var scopedServices = scope.ServiceProvider; var dbContext = scopedServices.GetRequiredService<Удалить_DataContext>(); var logger = scopedServices .GetRequiredService<ILogger<TestWebApplicationFactory<TStartup>>>(); try { new EfTestDbInitializer(dbContext).InitializeDb(); } catch (Exception ex) { logger.LogError(ex, "Проблема во время заполнения тестовой базы. " + "Ошибка: {Message}", ex.Message); } }); } } }