DeutschLernen/Tests/Unit/Application/Services/LessonServiceTests.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

161 lines
5.6 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 LessonServiceTests
{
private Mock<ILessonRepository> _mockLessonRepo;
private Mock<ILevelRepository> _mockLevelRepo;
private LessonService _service;
[TestInitialize]
public void Setup()
{
_mockLessonRepo = new Mock<ILessonRepository>();
_mockLevelRepo = new Mock<ILevelRepository>();
_service = new LessonService(_mockLessonRepo.Object, _mockLevelRepo.Object);
}
[TestMethod]
public async Task GetAllLessonsAsync_ReturnsMappedDtoList()
{
var lessons = new List<Lesson> { Lesson.Create(1, "Test", 1, "Topic", "Desc") };
_mockLessonRepo.Setup(r => r.GetAllAsync(It.IsAny<CancellationToken>()))
.ReturnsAsync(lessons);
var result = await _service.GetAllLessonsAsync();
Assert.AreEqual(1, result.Count);
Assert.AreEqual("Test", result[0].Title);
}
[TestMethod]
public async Task GetLessonByIdAsync_WithExistingId_ReturnsLesson()
{
var lesson = Lesson.Create(1, "Test", 1, "Topic", "Desc");
_mockLessonRepo.Setup(r => r.GetByIdAsync(1, It.IsAny<CancellationToken>()))
.ReturnsAsync(lesson);
var result = await _service.GetLessonByIdAsync(1);
Assert.IsNotNull(result);
Assert.AreEqual("Test", result.Title);
}
[TestMethod]
public async Task GetLessonByIdAsync_WithNonExistingId_ReturnsNull()
{
_mockLessonRepo.Setup(r => r.GetByIdAsync(999, It.IsAny<CancellationToken>()))
.ReturnsAsync((Lesson?)null);
var result = await _service.GetLessonByIdAsync(999);
Assert.IsNull(result);
}
[TestMethod]
public async Task GetLessonsByLevelAsync_ReturnsFilteredLessons()
{
var lessons = new List<Lesson> { Lesson.Create(1, "L1", 1, "T1", "D1") };
_mockLessonRepo.Setup(r => r.GetByLevelAsync(1, It.IsAny<CancellationToken>()))
.ReturnsAsync(lessons);
var result = await _service.GetLessonsByLevelAsync(1);
Assert.AreEqual(1, result.Count);
Assert.AreEqual("L1", result[0].Title);
}
[TestMethod]
public async Task CreateLessonAsync_WithValidDtoAndValidLevel_CreatesLesson()
{
var level = Level.Create("A1", "A1", 1);
var dto = new CreateLessonDto("New", "Desc", 1, 1, "Topic");
var lesson = Lesson.Create(1, "New", 1, "Topic", "Desc");
_mockLevelRepo.Setup(r => r.GetByIdAsync(1, It.IsAny<CancellationToken>()))
.ReturnsAsync(level);
_mockLessonRepo.Setup(r => r.AddAsync(It.IsAny<Lesson>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(lesson);
var result = await _service.CreateLessonAsync(dto);
Assert.IsNotNull(result);
Assert.AreEqual("New", result.Title);
}
[TestMethod]
public async Task UpdateLessonAsync_WithExistingLesson_UpdatesLesson()
{
var existing = Lesson.Create(1, "Old", 1, "OldT", "OldD");
var level = Level.Create("A1", "A1", 1);
var dto = new UpdateLessonDto("New", "NewD", 1, 1, "NewT");
_mockLessonRepo.Setup(r => r.GetByIdAsync(1, It.IsAny<CancellationToken>()))
.ReturnsAsync(existing);
_mockLevelRepo.Setup(r => r.GetByIdAsync(1, It.IsAny<CancellationToken>()))
.ReturnsAsync(level);
_mockLessonRepo.Setup(r => r.UpdateAsync(existing, It.IsAny<CancellationToken>()))
.Returns(Task.CompletedTask);
var result = await _service.UpdateLessonAsync(1, dto);
Assert.IsNotNull(result);
}
[TestMethod]
public async Task DeleteLessonAsync_WithExistingLesson_ReturnsTrue()
{
var lesson = Lesson.Create(1, "Test", 1, "Topic", "Desc");
_mockLessonRepo.Setup(r => r.GetByIdAsync(1, It.IsAny<CancellationToken>()))
.ReturnsAsync(lesson);
_mockLessonRepo.Setup(r => r.DeleteAsync(lesson, It.IsAny<CancellationToken>()))
.Returns(Task.CompletedTask);
var result = await _service.DeleteLessonAsync(1);
Assert.IsTrue(result);
}
[TestMethod]
public async Task GetBeginnerLessonsAsync_ReturnsA1AndA2Lessons()
{
var level1 = new List<Lesson> { Lesson.Create(1, "A1L1", 1, "T1", "D1") };
var level2 = new List<Lesson> { Lesson.Create(2, "A2L1", 1, "T2", "D2") };
_mockLessonRepo.Setup(r => r.GetByLevelAsync(1, It.IsAny<CancellationToken>()))
.ReturnsAsync(level1);
_mockLessonRepo.Setup(r => r.GetByLevelAsync(2, It.IsAny<CancellationToken>()))
.ReturnsAsync(level2);
var result = await _service.GetBeginnerLessonsAsync();
Assert.AreEqual(2, result.Count);
}
[TestMethod]
public async Task GetAdvancedLessonsAsync_ReturnsB2AndC1Lessons()
{
var level4 = new List<Lesson> { Lesson.Create(4, "B2L1", 1, "T4", "D4") };
var level5 = new List<Lesson> { Lesson.Create(5, "C1L1", 1, "T5", "D5") };
_mockLessonRepo.Setup(r => r.GetByLevelAsync(4, It.IsAny<CancellationToken>()))
.ReturnsAsync(level4);
_mockLessonRepo.Setup(r => r.GetByLevelAsync(5, It.IsAny<CancellationToken>()))
.ReturnsAsync(level5);
var result = await _service.GetAdvancedLessonsAsync();
Assert.AreEqual(2, result.Count);
}
}