Integration tests for ASP.NET Core APIs — WebApplicationFactory, xUnit, ConfigureTestServices, FluentAssertions, database isolation
97
96%
Does it follow best practices?
Impact
99%
1.45xAverage score across 5 eval scenarios
Passed
No known issues
The engineering team at ShopFast has an ASP.NET Core Order Management API that uses Entity Framework Core with SQL Server in production. The existing test suite uses an in-memory database, but the team has been hitting intermittent test failures when testing order-line item relationships — the in-memory provider silently ignores foreign key violations, allowing invalid data to be saved that would be rejected in production.
After investigating, the lead developer concluded that the test database needs to enforce relational constraints the same way SQL Server does. They've decided to switch the integration tests to use SQLite in-memory mode, which supports referential integrity, without requiring a real database server. Your task is to update the test project to use SQLite in-memory as the test database, ensuring the schema is initialized properly before tests run.
Produce the following files:
tests/OrderApi.Tests/TestWebApplicationFactory.cs — an updated WebApplicationFactory that configures the SQLite in-memory databasetests/OrderApi.Tests/OrderTests.cs — a test class with at least 2 tests that exercise the order and order-line endpointsThe grader will inspect the source code of these files.
The following files are provided as inputs. Extract them before beginning.
=============== FILE: inputs/src/OrderApi/Program.cs =============== using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args); builder.Services.AddDbContext<OrderDb>(opt => opt.UseSqlServer(builder.Configuration.GetConnectionString("Default")));
var app = builder.Build();
app.MapGet("/api/orders", async (OrderDb db) => Results.Ok(new { data = await db.Orders.Include(o => o.Lines).ToListAsync() }));
app.MapPost("/api/orders", async (OrderInput input, OrderDb db) => { if (input.Lines == null || input.Lines.Count == 0) return Results.BadRequest(new { error = new { message = "Order must have at least one line" } });
var order = new Order
{
CustomerName = input.CustomerName,
Lines = input.Lines.Select(l => new OrderLine { ProductId = l.ProductId, Qty = l.Qty }).ToList()
};
db.Orders.Add(order);
await db.SaveChangesAsync();
return Results.Created($"/api/orders/{order.Id}", new { data = order });});
app.Run();
=============== FILE: inputs/src/OrderApi/Models.cs =============== using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema;
public class Order { public int Id { get; set; } public string CustomerName { get; set; } = ""; public List<OrderLine> Lines { get; set; } = new(); }
public class OrderLine { public int Id { get; set; } [ForeignKey("Order")] public int OrderId { get; set; } public int ProductId { get; set; } public int Qty { get; set; } }
public record OrderInput(string CustomerName, List<LineInput> Lines); public record LineInput(int ProductId, int Qty);
=============== FILE: inputs/src/OrderApi/OrderDb.cs =============== using Microsoft.EntityFrameworkCore;
public class OrderDb : DbContext { public OrderDb(DbContextOptions<OrderDb> options) : base(options) { } public DbSet<Order> Orders => Set<Order>(); public DbSet<OrderLine> OrderLines => Set<OrderLine>(); }