LANCommander/LANCommander.Server.Tests/ApplicationFactory.cs
Pat Hartl 86c906f8db Add project for automated tests
Had to remove the responsibility of providing the current version of the application from UpdateService. A new IVersionProvider can be used to get the version. This also allows for this to be mocked in tests.
2025-03-15 21:00:00 -05:00

41 lines
No EOL
1.3 KiB
C#

using System.Data.Common;
using LANCommander.Server.Data;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.Extensions.DependencyInjection;
namespace LANCommander.Server.Tests;
public class ApplicationFactory<TProgram>
: WebApplicationFactory<TProgram> where TProgram : class
{
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.ConfigureServices(services =>
{
var dbContextDescriptor = services.SingleOrDefault(
d => d.ServiceType ==
typeof(IDbContextOptionsConfiguration<DatabaseContext>));
services.Remove(dbContextDescriptor);
var dbConnectionDescriptor = services.SingleOrDefault(
d => d.ServiceType ==
typeof(DbConnection));
services.Remove(dbConnectionDescriptor);
services.AddDbContextFactory<DatabaseContext>(optionsBuilder =>
{
optionsBuilder.UseInMemoryDatabase("Test");
});
services.AddDbContext<DatabaseContext>(optionsBuilder =>
{
optionsBuilder.UseInMemoryDatabase("Test");
});
});
}
}