forked from ddrilling/AsbCloudServer
88ce24b8bb
1. Создание инфраструктуры для интегарционных тестов 2. Покрытие контроллера месторождений тестами
52 lines
1.6 KiB
C#
52 lines
1.6 KiB
C#
using AsbCloudDb;
|
|
using AsbCloudDb.Model;
|
|
using AsbCloudWebApi.IntegrationTests.TestFakers;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.AspNetCore.Mvc.Testing;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Xunit;
|
|
|
|
namespace AsbCloudWebApi.IntegrationTests;
|
|
|
|
public class WebAppFactoryFixture : WebApplicationFactory<Startup>,
|
|
IAsyncLifetime
|
|
{
|
|
private const string connectionString =
|
|
"Host=localhost;Database=test;Port=5433;Username=postgres;Password=root;Persist Security Info=True;Include Error Detail=True;";
|
|
|
|
protected override void ConfigureWebHost(IWebHostBuilder builder)
|
|
{
|
|
builder.ConfigureServices(services =>
|
|
{
|
|
var descriptor = services.FirstOrDefault(d => d.ServiceType == typeof(DbContextOptions<AsbCloudDbContext>));
|
|
|
|
if (descriptor is not null)
|
|
services.Remove(descriptor);
|
|
|
|
services.AddDbContext<AsbCloudDbContext>(options =>
|
|
options.UseNpgsql(connectionString));
|
|
});
|
|
}
|
|
|
|
public async Task InitializeAsync()
|
|
{
|
|
using var scope = Services.CreateScope();
|
|
var scopedServices = scope.ServiceProvider;
|
|
var dbContext = scopedServices.GetRequiredService<AsbCloudDbContext>();
|
|
|
|
dbContext.Database.EnsureCreatedAndMigrated();
|
|
|
|
dbContext.Deposits.AddRange(DepositTestFaker.GetDeposits(15));
|
|
await dbContext.SaveChangesAsync();
|
|
}
|
|
|
|
public new async Task DisposeAsync()
|
|
{
|
|
using var scope = Services.CreateScope();
|
|
var scopedServices = scope.ServiceProvider;
|
|
var dbContext = scopedServices.GetRequiredService<AsbCloudDbContext>();
|
|
|
|
await dbContext.Database.EnsureDeletedAsync();
|
|
}
|
|
} |