Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Sqlite Provider for Functional Tests #639

Merged
merged 5 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,9 @@
namespace Clean.Architecture.FunctionalTests.ApiEndpoints;

[Collection("Sequential")]
public class ContributorGetById : IClassFixture<CustomWebApplicationFactory<Program>>
public class ContributorGetById(CustomWebApplicationFactory<Program> factory) : IClassFixture<CustomWebApplicationFactory<Program>>
{
private readonly HttpClient _client;

public ContributorGetById(CustomWebApplicationFactory<Program> factory)
{
_client = factory.CreateClient();
}
private readonly HttpClient _client = factory.CreateClient();

[Fact]
public async Task ReturnsSeedContributorGivenId1()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,9 @@
namespace Clean.Architecture.FunctionalTests.ApiEndpoints;

[Collection("Sequential")]
public class ContributorList : IClassFixture<CustomWebApplicationFactory<Program>>
public class ContributorList(CustomWebApplicationFactory<Program> factory) : IClassFixture<CustomWebApplicationFactory<Program>>
{
private readonly HttpClient _client;

public ContributorList(CustomWebApplicationFactory<Program> factory)
{
_client = factory.CreateClient();
}
private readonly HttpClient _client = factory.CreateClient();

[Fact]
public async Task ReturnsTwoContributors()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using Clean.Architecture.Infrastructure.Data;
using Clean.Architecture.Infrastructure.Data.Queries;
using Clean.Architecture.UseCases.Contributors.List;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -37,6 +34,10 @@ protected override IHost CreateHost(IHostBuilder builder)
var logger = scopedServices
.GetRequiredService<ILogger<CustomWebApplicationFactory<TProgram>>>();

// Reset Sqlite database for each test run
// If using a real database, you'll likely want to remove this step.
db.Database.EnsureDeleted();

// Ensure the database is created.
db.Database.EnsureCreated();

Expand Down Expand Up @@ -64,24 +65,26 @@ protected override void ConfigureWebHost(IWebHostBuilder builder)
builder
.ConfigureServices(services =>
{
// Remove the app's ApplicationDbContext registration.
var descriptor = services.SingleOrDefault(
d => d.ServiceType ==
typeof(DbContextOptions<AppDbContext>));
// Configure test dependencies here

//// Remove the app's ApplicationDbContext registration.
//var descriptor = services.SingleOrDefault(
//d => d.ServiceType ==
// typeof(DbContextOptions<AppDbContext>));

if (descriptor != null)
{
services.Remove(descriptor);
}
//if (descriptor != null)
//{
// services.Remove(descriptor);
//}

// This should be set for each individual test run
string inMemoryCollectionName = Guid.NewGuid().ToString();
//// This should be set for each individual test run
//string inMemoryCollectionName = Guid.NewGuid().ToString();

// Add ApplicationDbContext using an in-memory database for testing.
services.AddDbContext<AppDbContext>(options =>
{
options.UseInMemoryDatabase(inMemoryCollectionName);
});
//// Add ApplicationDbContext using an in-memory database for testing.
//services.AddDbContext<AppDbContext>(options =>
//{
// options.UseInMemoryDatabase(inMemoryCollectionName);
//});
});
}
}