ASP.NET Core 7 Web API integration tests – WithHostBuilder breaking database connection

I’m working on an ASP.NET Core 7 Web API, and I’m writing integration tests using CustomWebApplicationFactory.

My program is making some external HTTP calls, so I want to mock them in my tests. I want to have granular control on what I mock and to be able to setup HTTP return values from each test.

That’s why I’m using WithWebHostBuilder and then ConfigureTestServices to mock the HttpClient.

My problem is that the simple addition of the factory.WithHostBuilder call, the tests start to fail, as if the database doesn’t persist anything.

I’ll show you a simplified example that mimicks my approach, and in which the issue I describe persists.

My simple custom web application factory, replacing DbContext:

public class CustomWebApplicationFactory<TProgram> : WebApplicationFactory<TProgram> where TProgram : class
{
    protected override void ConfigureWebHost(IWebHostBuilder builder)
    {
        builder.ConfigureServices(services =>
        {
            ServiceDescriptor? descriptor = services.SingleOrDefault(
                d => d.ServiceType == typeof(DbContextOptions<SetsContext>));

            if (descriptor is not null)
            {
                services.Remove(descriptor);
            }

            ServiceDescriptor? dbConnectionDescriptor = services.SingleOrDefault(
                d => d.ServiceType ==
                     typeof(DbConnection));

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

            // Create open SqliteConnection so EF won't automatically close it.
            services.AddSingleton<DbConnection>(container =>
            {
                var connection = new SqliteConnection("DataSource=:memory:");
                connection.Open();

                return connection;
            });

            services.AddDbContext<SetsContext>((container, options) =>
            {
                DbConnection connection = container.GetRequiredService<DbConnection>();
                options.UseSqlite(connection);
            });
        });

        builder.UseEnvironment("test");
    }
}

My tests then use this factory:

public class SetsTests : IClassFixture<CustomWebApplicationFactory<Program>>
{
    private readonly CustomWebApplicationFactory<Program> _factory;

    public SetsTests(CustomWebApplicationFactory<Program> factory)
    {
        _factory = factory;
    }

    [Fact]
    public async Task Get_WithTwoSets_ReturnsJsonWithThem()
    {
        HttpClient client = _factory.CreateClient();

        await using (AsyncServiceScope scope = _factory.Services.CreateAsyncScope())
        {
            ISetsRepository setsRepository = scope.ServiceProvider.GetRequiredService<ISetsRepository>();
            SetDto set1 = await setsRepository.CreateSetAsync("123");
            SetDto set2 = await setsRepository.CreateSetAsync("456");
        }

        HttpResponseMessage response = await client.GetAsync("api/sets/");

        Assert.Equal(HttpStatusCode.OK, response.StatusCode);
        var respType = response.Content.Headers.ContentType?.MediaType;
        Assert.Equal("application/json", respType);
        SetDto[]? content = await GetResponseContentAsync<SetDto[]>(response);
        Assert.NotNull(content);
        Assert.Equal(2, content.Length);
    }
}

This simple test passes – I create 2 sets and then they are returned from the DB properly.

But just with one simple change (to inject mock services), the tests now fails on Assert.Equal(2, content.Length); check – returned content has 0 records.

// code breaks if I replace
// HttpClient client = _factory.CreateClient();
// with
HttpClient client = _factory.WithWebHostBuilder(builder => { }).CreateClient();

I have no idea why it’s causing that.

My Program.cs in my API is very simple in this example:

using BrickFolio.Data;

using Microsoft.EntityFrameworkCore;

WebApplicationBuilder builder = WebApplication.CreateBuilder(args);

builder.Services.AddControllers();

builder.Services.AddEndpointsApiExplorer();

builder.Services.AddDataServices(builder.Configuration, builder.Environment);

WebApplication app = builder.Build();

if (builder.Environment.IsProduction() is false)
{
    using IServiceScope scope = app.Services.CreateScope();
    IServiceProvider services = scope.ServiceProvider;

    try
    {
        SetsContext context = services.GetRequiredService<SetsContext>();
        context.Database.Migrate();
    }
    catch (Exception ex)
    {
        ILogger logger = services.GetRequiredService<ILogger>();
        logger.LogError(ex, "An error occurred while migrating the database.");
    }
}

app.MapControllers();

app.Run();

public partial class Program
{
}

It only sets up controllers and calls AddsDataServices from the data project:

public static IServiceCollection AddDataServices(this IServiceCollection services, IConfiguration config, IHostEnvironment env)
{
    DatabaseOptions? databaseOptions = config.GetRequiredSection(DatabaseOptions.SectionName)
            .Get<DatabaseOptions>();

    if (databaseOptions is null)
    {
        throw new ArgumentException("DatabaseOptions not found in configuration");
    }

    var dbPath = Path.Combine(env.ContentRootPath, databaseOptions.FileName);

    services.AddDbContext<SetsContext>(options =>
            options.UseSqlite($"Data Source={dbPath}"));

    services.AddScoped<ISetsRepository, SetsRepository>();

    return services;
}

That adds DbContext.

Am I understanding WithHostBuilder wrongly? Why would it result in this unexpected behaviour?

I have tried not using the repository and just using contexts in my tests, but the result was the same.

I’ve also tried debugging it a bit more, and using SQLite with files instead of the memory option. I’ve assigned an unique Guid to the test db in each test:

        services.AddSingleton<DbConnection>(container =>
        {
            var dbFileName = $"test-{Guid.NewGuid()}.db";
            var connection = new SqliteConnection($"Data Source={dbFileName}");
            connection.Open();

            return connection;
        });

As a result, I know that without calling WithHostBuilder, it only creates one test file that has the records.

When I call WithHostBuilder, I end up with two files, one with records and the second one empty. I suspect that the same happens when I use memory Data Source – I end up with two “in memory” database connections, with tests using one connection, and the API Program using the other.

I’m only unsure how to address that.

EDIT: I realised I haven’t shown my controller:

[ApiController]
[Route("api/sets")]
public class SetsController : ControllerBase
{
    private readonly ISetsRepository _setsRepository;

    public SetsController(ISetsRepository setsRepository)
    {
        _setsRepository = setsRepository;
    }

    // GET api/sets
    [HttpGet]
    [ProducesResponseType<SetDto>(StatusCodes.Status200OK)]
    [Produces("application/json")]
    public async Task<IList<SetDto>> Index()
    {
        IList<SetDto> sets = await _setsRepository.GetAllSetsAsync();
        return sets;
    }
}

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật