Skip to content

Commit

Permalink
Merge branch 'main' into KyleMcMaster-patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
KyleMcMaster authored Nov 27, 2023
2 parents f4df9f0 + b918945 commit 9603856
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 32 deletions.
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);
//});
});
}
}

0 comments on commit 9603856

Please sign in to comment.