fix: prevent potential component state updates after disposal and implement dedicated repository for quiz results
This commit is contained in:
+36
-44
@@ -5,53 +5,46 @@ using FluentAssertions;
|
||||
using Microsoft.Data.Sqlite;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Moq;
|
||||
using NexusReader.Application.Abstractions.Persistence;
|
||||
using NexusReader.Application.Commands.Quiz;
|
||||
using NexusReader.Data.Persistence;
|
||||
using NexusReader.Domain.Entities;
|
||||
using NexusReader.Infrastructure.Persistence;
|
||||
using Xunit;
|
||||
|
||||
namespace NexusReader.Application.Tests.Commands;
|
||||
|
||||
public class SubmitQuizResultCommandHandlerTests : IDisposable
|
||||
public class SubmitQuizResultCommandHandlerTests
|
||||
{
|
||||
private readonly SqliteConnection _connection;
|
||||
private readonly DbContextOptions<AppDbContext> _contextOptions;
|
||||
private readonly Mock<IDbContextFactory<AppDbContext>> _dbContextFactoryMock;
|
||||
private readonly Mock<IQuizResultRepository> _repositoryMock;
|
||||
|
||||
public SubmitQuizResultCommandHandlerTests()
|
||||
{
|
||||
_connection = new SqliteConnection("DataSource=:memory:");
|
||||
_connection.Open();
|
||||
|
||||
_contextOptions = new DbContextOptionsBuilder<AppDbContext>()
|
||||
.UseSqlite(_connection)
|
||||
.Options;
|
||||
|
||||
using var context = new AppDbContext(_contextOptions);
|
||||
context.Database.EnsureCreated();
|
||||
|
||||
_dbContextFactoryMock = new Mock<IDbContextFactory<AppDbContext>>();
|
||||
_dbContextFactoryMock.Setup(f => f.CreateDbContextAsync(It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(() => new AppDbContext(_contextOptions));
|
||||
_repositoryMock = new Mock<IQuizResultRepository>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_WithValidRequest_PersistsQuizResultToDatabase()
|
||||
{
|
||||
// Arrange
|
||||
using (var context = new AppDbContext(_contextOptions))
|
||||
var user = new NexusUser
|
||||
{
|
||||
var user = new NexusUser
|
||||
{
|
||||
Id = "user-abc",
|
||||
UserName = "testuser",
|
||||
Email = "test@example.com",
|
||||
TenantId = "tenant-xyz",
|
||||
SubscriptionPlanId = 1
|
||||
};
|
||||
context.Users.Add(user);
|
||||
await context.SaveChangesAsync();
|
||||
}
|
||||
Id = "user-abc",
|
||||
UserName = "testuser",
|
||||
Email = "test@example.com",
|
||||
TenantId = "tenant-xyz",
|
||||
SubscriptionPlanId = 1
|
||||
};
|
||||
|
||||
_repositoryMock.Setup(r => r.FindUserByIdAsync("user-abc", It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(user);
|
||||
|
||||
QuizResult? capturedQuizResult = null;
|
||||
_repositoryMock.Setup(r => r.AddQuizResult(It.IsAny<QuizResult>()))
|
||||
.Callback<QuizResult>(q => capturedQuizResult = q);
|
||||
|
||||
_repositoryMock.Setup(r => r.SaveChangesAsync(It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(1);
|
||||
|
||||
var command = new SubmitQuizResultCommand(
|
||||
UserId: "user-abc",
|
||||
@@ -60,29 +53,30 @@ public class SubmitQuizResultCommandHandlerTests : IDisposable
|
||||
TotalQuestions: 5
|
||||
);
|
||||
|
||||
var handler = new SubmitQuizResultCommandHandler(_dbContextFactoryMock.Object);
|
||||
var handler = new SubmitQuizResultCommandHandler(_repositoryMock.Object);
|
||||
|
||||
// Act
|
||||
var result = await handler.Handle(command, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
result.IsSuccess.Should().BeTrue();
|
||||
capturedQuizResult.Should().NotBeNull();
|
||||
capturedQuizResult!.Topic.Should().Be("Sprawdzian: .NET 10");
|
||||
capturedQuizResult.Score.Should().Be(4);
|
||||
capturedQuizResult.TotalQuestions.Should().Be(5);
|
||||
capturedQuizResult.TenantId.Should().Be("tenant-xyz");
|
||||
|
||||
using (var context = new AppDbContext(_contextOptions))
|
||||
{
|
||||
var quizResult = await context.QuizResults.FirstOrDefaultAsync(q => q.UserId == "user-abc");
|
||||
quizResult.Should().NotBeNull();
|
||||
quizResult!.Topic.Should().Be("Sprawdzian: .NET 10");
|
||||
quizResult.Score.Should().Be(4);
|
||||
quizResult.TotalQuestions.Should().Be(5);
|
||||
quizResult.TenantId.Should().Be("tenant-xyz");
|
||||
}
|
||||
_repositoryMock.Verify(r => r.AddQuizResult(It.IsAny<QuizResult>()), Times.Once);
|
||||
_repositoryMock.Verify(r => r.SaveChangesAsync(It.IsAny<CancellationToken>()), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_WithNonExistentUser_ReturnsFailureResult()
|
||||
{
|
||||
// Arrange
|
||||
_repositoryMock.Setup(r => r.FindUserByIdAsync("non-existent", It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync((NexusUser?)null);
|
||||
|
||||
var command = new SubmitQuizResultCommand(
|
||||
UserId: "non-existent",
|
||||
Topic: "Sprawdzian: .NET 10",
|
||||
@@ -90,7 +84,7 @@ public class SubmitQuizResultCommandHandlerTests : IDisposable
|
||||
TotalQuestions: 5
|
||||
);
|
||||
|
||||
var handler = new SubmitQuizResultCommandHandler(_dbContextFactoryMock.Object);
|
||||
var handler = new SubmitQuizResultCommandHandler(_repositoryMock.Object);
|
||||
|
||||
// Act
|
||||
var result = await handler.Handle(command, CancellationToken.None);
|
||||
@@ -98,10 +92,8 @@ public class SubmitQuizResultCommandHandlerTests : IDisposable
|
||||
// Assert
|
||||
result.IsFailed.Should().BeTrue();
|
||||
result.Errors.Should().ContainSingle(e => e.Message == "User not found.");
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_connection.Dispose();
|
||||
_repositoryMock.Verify(r => r.AddQuizResult(It.IsAny<QuizResult>()), Times.Never);
|
||||
_repositoryMock.Verify(r => r.SaveChangesAsync(It.IsAny<CancellationToken>()), Times.Never);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user