From f2bb2d984b84b9e788668ecf17470d4033be7e4a Mon Sep 17 00:00:00 2001 From: Assistant builder Date: Thu, 11 Jun 2026 18:17:48 +0000 Subject: [PATCH] Add ApiFactory test helper + /health sanity test --- backend.tests/ApiFactory.cs | 33 +++++++++++++++++++++++++++++++++ backend.tests/HealthTests.cs | 19 +++++++++++++++++++ backend.tests/UnitTest1.cs | 10 ---------- 3 files changed, 52 insertions(+), 10 deletions(-) create mode 100644 backend.tests/ApiFactory.cs create mode 100644 backend.tests/HealthTests.cs delete mode 100644 backend.tests/UnitTest1.cs diff --git a/backend.tests/ApiFactory.cs b/backend.tests/ApiFactory.cs new file mode 100644 index 0000000..96842ff --- /dev/null +++ b/backend.tests/ApiFactory.cs @@ -0,0 +1,33 @@ +using System.IO; +using backend.Data; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Mvc.Testing; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; + +namespace backend.tests; + +public class ApiFactory : WebApplicationFactory +{ + private readonly string _dbPath = Path.Combine(Path.GetTempPath(), $"assistant-test-{Guid.NewGuid():N}.db"); + + protected override void ConfigureWebHost(IWebHostBuilder builder) + { + builder.UseEnvironment("Test"); + builder.ConfigureAppConfiguration((_, cfg) => + { + cfg.AddInMemoryCollection(new Dictionary + { + ["ConnectionStrings:Default"] = $"Data Source={_dbPath}", + }); + }); + } + + protected override void Dispose(bool disposing) + { + base.Dispose(disposing); + if (File.Exists(_dbPath)) + File.Delete(_dbPath); + } +} diff --git a/backend.tests/HealthTests.cs b/backend.tests/HealthTests.cs new file mode 100644 index 0000000..9d097ba --- /dev/null +++ b/backend.tests/HealthTests.cs @@ -0,0 +1,19 @@ +using System.Net; +using FluentAssertions; +using Xunit; + +namespace backend.tests; + +public class HealthTests(ApiFactory factory) : IClassFixture +{ + private readonly ApiFactory _factory = factory; + + [Fact] + public async Task Health_returns_ok() + { + var client = _factory.CreateClient(); + var res = await client.GetAsync("/health"); + res.StatusCode.Should().Be(HttpStatusCode.OK); + (await res.Content.ReadAsStringAsync()).Should().Contain("\"ok\":true"); + } +} diff --git a/backend.tests/UnitTest1.cs b/backend.tests/UnitTest1.cs deleted file mode 100644 index 8463914..0000000 --- a/backend.tests/UnitTest1.cs +++ /dev/null @@ -1,10 +0,0 @@ -namespace backend.tests; - -public class UnitTest1 -{ - [Fact] - public void Test1() - { - - } -}