DeutschLernen/Tests/Unit/Application/Services/LevelServiceTests.cs
Lasse Rune Hansen 0ff6218ff1 test(backend): add unit tests for LevelService, LessonService, and ProgressService
- Created LevelServiceTests.cs with 10 test methods
- Created LessonServiceTests.cs with 10 test methods
- Created ProgressServiceTests.cs with 10 test methods
- All tests use Moq for repository mocking
- Total: 30 new unit tests for application services
- All tests follow MSTest framework conventions

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
2026-06-09 06:50:13 +02:00

143 lines
4.5 KiB
C#

using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using GermanApp.Application.DTOs;
using GermanApp.Application.Services;
using GermanApp.Domain.Entities;
using GermanApp.Domain.Interfaces;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
namespace GermanApp.Tests.Unit.Application.Services;
[TestClass]
public class LevelServiceTests
{
private Mock<ILevelRepository> _mockRepo;
private LevelService _service;
[TestInitialize]
public void Setup()
{
_mockRepo = new Mock<ILevelRepository>();
_service = new LevelService(_mockRepo.Object);
}
[TestMethod]
public async Task GetAllLevelsAsync_ReturnsMappedDtoList()
{
var levels = new List<Level> { Level.Create("A1", "A1", 1) };
_mockRepo.Setup(r => r.GetAllOrderedAsync(It.IsAny<CancellationToken>()))
.ReturnsAsync(levels);
var result = await _service.GetAllLevelsAsync();
Assert.AreEqual(1, result.Count);
Assert.AreEqual("A1", result[0].Name);
}
[TestMethod]
public async Task GetLevelByIdAsync_WithExistingId_ReturnsLevel()
{
var level = Level.Create("A1", "A1", 1);
_mockRepo.Setup(r => r.GetByIdAsync(1, It.IsAny<CancellationToken>()))
.ReturnsAsync(level);
var result = await _service.GetLevelByIdAsync(1);
Assert.IsNotNull(result);
Assert.AreEqual("A1", result.Name);
}
[TestMethod]
public async Task GetLevelByIdAsync_WithNonExistingId_ReturnsNull()
{
_mockRepo.Setup(r => r.GetByIdAsync(999, It.IsAny<CancellationToken>()))
.ReturnsAsync((Level?)null);
var result = await _service.GetLevelByIdAsync(999);
Assert.IsNull(result);
}
[TestMethod]
public async Task GetLevelByCodeAsync_WithExistingCode_ReturnsLevel()
{
var level = Level.Create("A1", "A1", 1);
_mockRepo.Setup(r => r.GetByCodeAsync("A1", It.IsAny<CancellationToken>()))
.ReturnsAsync(level);
var result = await _service.GetLevelByCodeAsync("A1");
Assert.IsNotNull(result);
Assert.AreEqual("A1", result.Code);
}
[TestMethod]
public async Task CreateLevelAsync_WithValidDto_CallsAddAsync()
{
var dto = new CreateLevelDto("Test", "T1", 1);
var level = Level.Create("Test", "T1", 1);
_mockRepo.Setup(r => r.AddAsync(It.IsAny<Level>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(level);
var result = await _service.CreateLevelAsync(dto);
Assert.IsNotNull(result);
_mockRepo.Verify(r => r.AddAsync(It.IsAny<Level>(), It.IsAny<CancellationToken>()), Times.Once);
}
[TestMethod]
public async Task UpdateLevelAsync_WithExistingLevel_CallsUpdateAsync()
{
var existing = Level.Create("A1", "A1", 1);
var dto = new UpdateLevelDto("New", "N1", 2);
_mockRepo.Setup(r => r.GetByIdAsync(1, It.IsAny<CancellationToken>()))
.ReturnsAsync(existing);
_mockRepo.Setup(r => r.UpdateAsync(existing, It.IsAny<CancellationToken>()))
.Returns(Task.CompletedTask);
var result = await _service.UpdateLevelAsync(1, dto);
Assert.IsNotNull(result);
_mockRepo.Verify(r => r.UpdateAsync(existing, It.IsAny<CancellationToken>()), Times.Once);
}
[TestMethod]
public async Task UpdateLevelAsync_WithNonExistingLevel_ReturnsNull()
{
var dto = new UpdateLevelDto("New", "N1", 2);
_mockRepo.Setup(r => r.GetByIdAsync(999, It.IsAny<CancellationToken>()))
.ReturnsAsync((Level?)null);
var result = await _service.UpdateLevelAsync(999, dto);
Assert.IsNull(result);
}
[TestMethod]
public async Task DeleteLevelAsync_WithExistingLevel_ReturnsTrue()
{
var level = Level.Create("A1", "A1", 1);
_mockRepo.Setup(r => r.GetByIdAsync(1, It.IsAny<CancellationToken>()))
.ReturnsAsync(level);
_mockRepo.Setup(r => r.DeleteAsync(level, It.IsAny<CancellationToken>()))
.Returns(Task.CompletedTask);
var result = await _service.DeleteLevelAsync(1);
Assert.IsTrue(result);
}
[TestMethod]
public async Task DeleteLevelAsync_WithNonExistingLevel_ReturnsFalse()
{
_mockRepo.Setup(r => r.GetByIdAsync(999, It.IsAny<CancellationToken>()))
.ReturnsAsync((Level?)null);
var result = await _service.DeleteLevelAsync(999);
Assert.IsFalse(result);
}
}