using FluentAssertions; using Microsoft.AspNetCore.Mvc; using Moq; using server.Controllers.v1; using server.Models; using server.Persistence.Repositories; using System.Net; namespace server.tests.v1.UnitTests { public class SeasonsControllerUnitTests { private readonly Mock _mockRepo; private readonly SeasonsController _controller; public SeasonsControllerUnitTests() { _mockRepo = new Mock(); _controller = new SeasonsController(_mockRepo.Object); } [Fact] public async Task GetSeasonsAsync_AllSeasons_Returns200StatusCodeWithSeasonsCollection() { var seasons = new List { new Season(), new Season() }; _mockRepo .Setup(repo => repo.GetSeasonsAsync()) .ReturnsAsync(seasons); var response = await _controller.GetSeasonsAsync() as ObjectResult; var data = response.Value as List; _mockRepo.Verify(repo => repo.GetSeasonsAsync(), Times.Once()); response.Should().NotBeNull(); response.Should().BeOfType(); response.StatusCode.Should().Be((int)HttpStatusCode.OK); data.Should().NotBeNull(); data.Should().BeOfType>(); data.Should().HaveCount(2); } [Fact] public async Task GetSeasonsAsync_RepoThrowsException_Returns500StatusCodeWithProblemDetails() { _mockRepo .Setup(repo => repo.GetSeasonsAsync()) .Throws(new Exception()); var response = await _controller.GetSeasonsAsync() as ObjectResult; var details = response.Value; _mockRepo.Verify(repo => repo.GetSeasonsAsync(), Times.Once()); response.Should().NotBeNull(); response.Should().BeOfType(); response.StatusCode.Should().Be((int)HttpStatusCode.InternalServerError); details.Should().NotBeNull(); details.Should().BeOfType(); } [Fact] public async Task GetSeasonByNumberAsync_ValidSeasonNumber_Returns200StatusCodeWithSeason() { var seasonNumber = 1; _mockRepo .Setup(repo => repo.GetSeasonByNumberAsync(seasonNumber)) .ReturnsAsync(new Season()); var response = await _controller.GetSeasonByNumberAsync(seasonNumber) as ObjectResult; var season = response.Value; _mockRepo.Verify(repo => repo.GetSeasonByNumberAsync(It.IsAny()), Times.Once()); response.Should().NotBeNull(); response.Should().BeOfType(); response.StatusCode.Should().Be((int)HttpStatusCode.OK); season.Should().NotBeNull(); season.Should().BeOfType(); } [Fact] public async Task GetSeasonByNumberAsync_ValidSeasonNumberForNonExistentSeason_Returns404StatusCodeWithProblemDetails() { var seasonNumber = 20; _mockRepo .Setup(repo => repo.GetSeasonByNumberAsync(seasonNumber)) .ReturnsAsync(null as Season); var response = await _controller.GetSeasonByNumberAsync(seasonNumber) as ObjectResult; var details = response.Value; _mockRepo.Verify(repo => repo.GetSeasonByNumberAsync(It.IsAny()), Times.Once()); response.Should().NotBeNull(); response.Should().BeOfType(); response.StatusCode.Should().Be((int)HttpStatusCode.NotFound); details.Should().NotBeNull(); details.Should().BeOfType(); } [Fact] public async Task GetSeasonByNumberAsync_RepoThrowsException_Returns500StatusCodeWithProblemDetails() { var seasonNumber = 1; _mockRepo .Setup(repo => repo.GetSeasonByNumberAsync(seasonNumber)) .Throws(new Exception()); var response = await _controller.GetSeasonByNumberAsync(seasonNumber) as ObjectResult; var details = response.Value; _mockRepo.Verify(repo => repo.GetSeasonByNumberAsync(It.IsAny()), Times.Once()); response.Should().NotBeNull(); response.Should().BeOfType(); response.StatusCode.Should().Be((int)HttpStatusCode.InternalServerError); details.Should().NotBeNull(); details.Should().BeOfType(); } } }