KoogleApp/test/Koogle.Tests/Integration/WebApplicationFactoryFixtur...

47 lines
1.4 KiB
C#

using Koogle.Infrastructure.Identity;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using KoogleApp.Data;
namespace Koogle.Tests.Integration;
/// <summary>
/// Custom WebApplicationFactory for integration testing.
/// Uses the Web project's Program class via assembly reference.
/// </summary>
/// <remarks>
/// To use this fixture, add `[assembly: InternalsVisibleTo("Koogle.Tests")]`
/// to the Koogle.Web project, or expose Program class publicly.
/// Alternatively, use the standalone integration test approach below.
/// </remarks>
public class StandaloneIntegrationTestBase : IAsyncLifetime
{
protected AppDbContext Context { get; private set; } = null!;
public Task InitializeAsync()
{
var options = new DbContextOptionsBuilder<AppDbContext>()
.UseInMemoryDatabase($"TestDb_{Guid.NewGuid()}")
.Options;
Context = new AppDbContext(options);
return Task.CompletedTask;
}
public async Task DisposeAsync()
{
await Context.DisposeAsync();
}
}
/// <summary>
/// Collection fixture for sharing test database across integration tests.
/// </summary>
[CollectionDefinition("Integration")]
public class IntegrationTestCollection : ICollectionFixture<StandaloneIntegrationTestBase>
{
}