Refactor repeated test host setup

This commit is contained in:
2026-04-01 10:48:28 +02:00
parent 18d1de45cb
commit 517c42250d
10 changed files with 74 additions and 140 deletions
@@ -3,12 +3,12 @@ using JobTrackerApi.Controllers;
using JobTrackerApi.Data;
using JobTrackerApi.Models;
using JobTrackerApi.Services;
using JobTrackerApi.Tests.TestSupport;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
using Moq;
using Xunit;
@@ -475,29 +475,12 @@ public sealed class JobApplicationsApplicationPackageTests
private static JobTrackerContext CreateDb()
{
var options = new DbContextOptionsBuilder<JobTrackerContext>()
.UseInMemoryDatabase(Guid.NewGuid().ToString())
.Options;
var currentUser = new Mock<ICurrentUserService>();
currentUser.SetupGet(service => service.UserId).Returns("user-1");
return new JobTrackerContext(options, currentUser.Object);
return TestHostFactory.CreateInMemoryDb();
}
private static Mock<UserManager<ApplicationUser>> CreateUserManager(ApplicationUser? user = null)
{
var store = new Mock<IUserStore<ApplicationUser>>();
var manager = new Mock<UserManager<ApplicationUser>>(
store.Object,
Options.Create(new IdentityOptions()),
new PasswordHasher<ApplicationUser>(),
Array.Empty<IUserValidator<ApplicationUser>>(),
Array.Empty<IPasswordValidator<ApplicationUser>>(),
new UpperInvariantLookupNormalizer(),
new IdentityErrorDescriber(),
null!,
new NullLogger<UserManager<ApplicationUser>>());
manager.Setup(x => x.FindByIdAsync(It.IsAny<string>())).ReturnsAsync((string id) => user is not null && user.Id == id ? user : null);
return manager;
return TestHostFactory.CreateUserManager(user);
}
private sealed class TestCvTemplateRenderer : ICvTemplateRenderer