Files
eShopOnWeb/tests/FunctionalTests/Web/WebTestFixture.cs
Said Haouaouchi 4304545f19 Fix Multiple DbContext Registration (#703)
Hi, i noticed that in the WebTestFixture used for FunctionalTests the DbContexts were registered without removing the existing ones in Program.cs
which leads to duplication
2023-02-28 15:16:39 -05:00

57 lines
2.0 KiB
C#

using System;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.EntityFrameworkCore;
using Microsoft.eShopWeb.Infrastructure.Data;
using Microsoft.eShopWeb.Infrastructure.Identity;
using Microsoft.eShopWeb.Web.Interfaces;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace Microsoft.eShopWeb.FunctionalTests.Web;
public class TestApplication : WebApplicationFactory<IBasketViewModelService>
{
private readonly string _environment = "Development";
protected override IHost CreateHost(IHostBuilder builder)
{
builder.UseEnvironment(_environment);
// Add mock/test services to the builder here
builder.ConfigureServices(services =>
{
var descriptors = services.Where(d =>
d.ServiceType == typeof(DbContextOptions<CatalogContext>) ||
d.ServiceType == typeof(DbContextOptions<AppIdentityDbContext>))
.ToList();
foreach (var descriptor in descriptors)
{
services.Remove(descriptor);
}
services.AddScoped(sp =>
{
// Replace SQLite with in-memory database for tests
return new DbContextOptionsBuilder<CatalogContext>()
.UseInMemoryDatabase("InMemoryDbForTesting")
.UseApplicationServiceProvider(sp)
.Options;
});
services.AddScoped(sp =>
{
// Replace SQLite with in-memory database for tests
return new DbContextOptionsBuilder<AppIdentityDbContext>()
.UseInMemoryDatabase("Identity")
.UseApplicationServiceProvider(sp)
.Options;
});
});
return base.CreateHost(builder);
}
}