autobus-api_old/AutobusApi.IntegrationTests/CustomWebApplicationFactory.cs
2023-11-15 19:00:34 +02:00

47 lines
1.5 KiB
C#

using System.Data.Common;
using AutobusApi.Infrastructure.Identity;
using AutoubsApi.Infrastructure.Data;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.EntityFrameworkCore;
namespace AutobusApi.IntegrationTests;
public class CustomWebApplicationFactory<TProgram>
: WebApplicationFactory<TProgram> where TProgram : class
{
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.ConfigureServices(services =>
{
var dbContextDescriptor = services.SingleOrDefault(
d => d.ServiceType ==
typeof(DbContextOptions<ApplicationDbContext>));
services.Remove(dbContextDescriptor);
var identityDbContextDescriptor = services.SingleOrDefault(
d => d.ServiceType ==
typeof(DbContextOptions<ApplicationIdentityDbContext>));
services.Remove(identityDbContextDescriptor);
var dbConnectionDescriptor = services.SingleOrDefault(
d => d.ServiceType ==
typeof(DbConnection));
services.Remove(dbConnectionDescriptor);
services.AddDbContext<ApplicationDbContext>((container, options) =>
{
options.UseInMemoryDatabase("autobus");
});
services.AddDbContext<ApplicationIdentityDbContext>((container, options) =>
{
options.UseInMemoryDatabase("autobus");
});
});
builder.UseEnvironment("Development");
}}