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 QuotesControllerUnitTests { private readonly Mock _mockRepo; private readonly QuotesController _controller; public QuotesControllerUnitTests() { _mockRepo = new Mock(); _controller = new QuotesController(_mockRepo.Object); } [Fact] public async Task GetQuotesAsync_AllQuotes_Returns200StatusCodeWithQuotesCollection() { var filter = new QuoteFilter(); var quotes = new List { new Quote(), new Quote() }; _mockRepo .Setup(repo => repo.GetQuotesAsync(filter)) .ReturnsAsync(quotes); var response = await _controller.GetQuotesAsync(filter) as ObjectResult; var data = response.Value as List; _mockRepo.Verify(repo => repo.GetQuotesAsync(It.IsAny()), 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 GetQuotesAsync_RepoThrowsException_Returns500StatusCodeWithProblemDetails() { var filter = new QuoteFilter(); _mockRepo .Setup(repo => repo.GetQuotesAsync(filter)) .Throws(new Exception()); var response = await _controller.GetQuotesAsync(filter) as ObjectResult; var details = response.Value; _mockRepo.Verify(repo => repo.GetQuotesAsync(It.IsAny()), 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 GetQuoteByIdAsync_ValidId_Returns200StatusCodeWithQuote() { var quoteId = "62b7d5506c1b407771829926"; _mockRepo .Setup(repo => repo.GetQuoteByIdAsync(quoteId)) .ReturnsAsync(new Quote()); var response = await _controller.GetQuoteByIdAsync(quoteId) as ObjectResult; var character = response.Value; _mockRepo.Verify(repo => repo.GetQuoteByIdAsync(It.IsAny()), Times.Once()); response.Should().NotBeNull(); response.Should().BeOfType(); response.StatusCode.Should().Be((int)HttpStatusCode.OK); character.Should().NotBeNull(); character.Should().BeOfType(); } public async Task GetQuoteByIdAsync_ValidIdForNonExistentQuote_Returns404StatusCodeWithProblemDetails() { var quoteId = "62b7d5506c1b407771829926"; _mockRepo .Setup(repo => repo.GetQuoteByIdAsync(quoteId)) .ReturnsAsync(null as Quote); var response = await _controller.GetQuoteByIdAsync(quoteId) as ObjectResult; var quote = response.Value; _mockRepo.Verify(repo => repo.GetQuoteByIdAsync(It.IsAny()), Times.Once()); response.Should().NotBeNull(); response.Should().BeOfType(); response.StatusCode.Should().Be((int)HttpStatusCode.NotFound); quote.Should().NotBeNull(); quote.Should().BeOfType(); } [Fact] public async Task GetQuoteByIdAsync_RepoThrowsException_Returns500StatusCodeWithProblemDetails() { var quoteId = "62b7d5506c1b407771829926"; _mockRepo .Setup(repo => repo.GetQuoteByIdAsync(quoteId)) .Throws(new Exception()); var response = await _controller.GetQuoteByIdAsync(quoteId) as ObjectResult; var details = response.Value; _mockRepo.Verify(repo => repo.GetQuoteByIdAsync(It.IsAny()), Times.Once()); response.Should().NotBeNull(); response.Should().BeOfType(); response.StatusCode.Should().Be((int)HttpStatusCode.InternalServerError); details.Should().NotBeNull(); details.Should().BeOfType(); } } }