Refactor nunit tests off InMemory/WebApplicationFactory, remove tuple returns (#4)
## Summary - Extract `IMicCheckDbContext` and mock DB access with Moq instead of the EF InMemory provider, per CLAUDE.md testing guidelines. - Rewrite HTTP-pipeline tests to exercise controllers/auth handlers directly instead of `WebApplicationFactory`. - Remove tuple return types across the API in favor of named records (`PagedResult<T>`, `ProfileUpdateResult`, `ApiKeyCreationResult`). ## Test plan - [x] `dotnet build` (full solution) - [x] `dotnet test tests/api/MicCheck.Api.Tests.Unit` — 187/187 pass - [x] `dotnet test tests/api/MicCheck.Api.Tests.Integration` — 8/8 pass (live API + Postgres via Aspire) - [x] Verified Aspire AppHost/dashboard starts and API responds Reviewed-on: #4
This commit was merged in pull request #4.
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
using System.Linq.Expressions;
|
||||
using MicCheck.Api.Data;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Moq;
|
||||
|
||||
namespace MicCheck.Api.Tests.Unit.TestSupport;
|
||||
|
||||
public static class MockDbSetFactory
|
||||
{
|
||||
public static Mock<DbSet<T>> Create<T>(List<T> data) where T : class
|
||||
{
|
||||
var queryable = data.AsQueryable();
|
||||
var mockSet = new Mock<DbSet<T>>();
|
||||
|
||||
mockSet.As<IAsyncEnumerable<T>>()
|
||||
.Setup(m => m.GetAsyncEnumerator(It.IsAny<CancellationToken>()))
|
||||
.Returns(() => new TestAsyncEnumerator<T>(data.GetEnumerator()));
|
||||
|
||||
mockSet.As<IQueryable<T>>().Setup(m => m.Provider).Returns(() => new TestAsyncQueryProvider<T>(data.AsQueryable().Provider));
|
||||
mockSet.As<IQueryable<T>>().Setup(m => m.Expression).Returns(() => data.AsQueryable().Expression);
|
||||
mockSet.As<IQueryable<T>>().Setup(m => m.ElementType).Returns(queryable.ElementType);
|
||||
mockSet.As<IQueryable<T>>().Setup(m => m.GetEnumerator()).Returns(() => data.GetEnumerator());
|
||||
|
||||
mockSet.Setup(m => m.Add(It.IsAny<T>())).Callback<T>(item => data.Add(item));
|
||||
mockSet.Setup(m => m.AddRange(It.IsAny<IEnumerable<T>>())).Callback<IEnumerable<T>>(items => data.AddRange(items));
|
||||
mockSet.Setup(m => m.Remove(It.IsAny<T>())).Callback<T>(item => data.Remove(item));
|
||||
mockSet.Setup(m => m.RemoveRange(It.IsAny<IEnumerable<T>>())).Callback<IEnumerable<T>>(items =>
|
||||
{
|
||||
foreach (var item in items.ToList())
|
||||
data.Remove(item);
|
||||
});
|
||||
|
||||
return mockSet;
|
||||
}
|
||||
|
||||
public static void SetupDbSet<T>(this Mock<IMicCheckDbContext> context, Expression<Func<IMicCheckDbContext, DbSet<T>>> selector, List<T> data)
|
||||
where T : class =>
|
||||
context.Setup(selector).Returns(Create(data).Object);
|
||||
|
||||
/// <summary>
|
||||
/// Sets up a DbSet whose Add() simulates database-generated identity assignment (needed because
|
||||
/// Moq does not run SaveChanges and entity Id properties are commonly init-only).
|
||||
/// </summary>
|
||||
public static void SetupDbSetWithGeneratedIds<T>(this Mock<IMicCheckDbContext> context, Expression<Func<IMicCheckDbContext, DbSet<T>>> selector, List<T> data)
|
||||
where T : class
|
||||
{
|
||||
var idProperty = typeof(T).GetProperty("Id") ?? throw new InvalidOperationException($"{typeof(T).Name} has no Id property.");
|
||||
var mockSet = Create(data);
|
||||
mockSet.Setup(m => m.Add(It.IsAny<T>())).Callback<T>(item =>
|
||||
{
|
||||
idProperty.SetValue(item, data.Count + 1);
|
||||
data.Add(item);
|
||||
});
|
||||
context.Setup(selector).Returns(mockSet.Object);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
using System.Collections;
|
||||
using System.Linq.Expressions;
|
||||
|
||||
namespace MicCheck.Api.Tests.Unit.TestSupport;
|
||||
|
||||
public class TestAsyncEnumerable<T> : EnumerableQuery<T>, IAsyncEnumerable<T>, IQueryable<T>
|
||||
{
|
||||
public TestAsyncEnumerable(IEnumerable<T> enumerable) : base(enumerable) { }
|
||||
|
||||
public TestAsyncEnumerable(Expression expression) : base(expression) { }
|
||||
|
||||
public IAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken cancellationToken = default) =>
|
||||
new TestAsyncEnumerator<T>(this.AsEnumerable().GetEnumerator());
|
||||
|
||||
IQueryProvider IQueryable.Provider => new TestAsyncQueryProvider<T>(this);
|
||||
}
|
||||
|
||||
public sealed class TestAsyncEnumerator<T>(IEnumerator<T> inner) : IAsyncEnumerator<T>
|
||||
{
|
||||
public T Current => inner.Current;
|
||||
|
||||
public ValueTask<bool> MoveNextAsync() => ValueTask.FromResult(inner.MoveNext());
|
||||
|
||||
public ValueTask DisposeAsync()
|
||||
{
|
||||
inner.Dispose();
|
||||
return ValueTask.CompletedTask;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
using System.Linq.Expressions;
|
||||
using Microsoft.EntityFrameworkCore.Query;
|
||||
|
||||
namespace MicCheck.Api.Tests.Unit.TestSupport;
|
||||
|
||||
public class TestAsyncQueryProvider<T>(IQueryProvider innerProvider) : IAsyncQueryProvider
|
||||
{
|
||||
public IQueryable CreateQuery(Expression expression) =>
|
||||
new TestAsyncEnumerable<T>(StripIncludes(expression));
|
||||
|
||||
public IQueryable<TElement> CreateQuery<TElement>(Expression expression) =>
|
||||
new TestAsyncEnumerable<TElement>(StripIncludes(expression));
|
||||
|
||||
public object? Execute(Expression expression) =>
|
||||
innerProvider.Execute(StripIncludes(expression));
|
||||
|
||||
public TResult Execute<TResult>(Expression expression) =>
|
||||
innerProvider.Execute<TResult>(StripIncludes(expression));
|
||||
|
||||
public TResult ExecuteAsync<TResult>(Expression expression, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var stripped = StripIncludes(expression);
|
||||
var resultType = typeof(TResult).GetGenericArguments() is [var inner] ? inner : typeof(TResult);
|
||||
var executionResult = typeof(IQueryProvider)
|
||||
.GetMethod(nameof(IQueryProvider.Execute), 1, [typeof(Expression)])!
|
||||
.MakeGenericMethod(resultType)
|
||||
.Invoke(innerProvider, [stripped]);
|
||||
|
||||
return (TResult)typeof(Task).GetMethod(nameof(Task.FromResult))!
|
||||
.MakeGenericMethod(resultType)
|
||||
.Invoke(null, [executionResult])!;
|
||||
}
|
||||
|
||||
private static Expression StripIncludes(Expression expression) => new IncludeStripVisitor().Visit(expression);
|
||||
|
||||
private sealed class IncludeStripVisitor : ExpressionVisitor
|
||||
{
|
||||
protected override Expression VisitMethodCall(MethodCallExpression node) =>
|
||||
node.Method.Name is "Include" or "ThenInclude"
|
||||
? Visit(node.Arguments[0])
|
||||
: base.VisitMethodCall(node);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user