using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using LeedsBeerQuest.API.Controllers; using LeedsBeerQuest.API.Data.Models; using LeedsBeerQuest.API.Data.Services.Interfaces; using LeedsBeerQuest.API.Tests.Data.Services; using LeedsBeerQuest.API.Tests.TestHelpers; using Microsoft.AspNetCore.Mvc; using NSubstitute; using NSubstitute.ReturnsExtensions; using NUnit.Framework; namespace LeedsBeerQuest.API.Tests.Controllers; [TestFixture] public class VenueControllerTests { private IVenueService _venueService; private VenueController _venueController; [SetUp] public void SetUp() { _venueService = Substitute.For(); _venueController = new VenueController(_venueService); } [Test] public async Task GetAll_ReturnsOk_WithAllVenues_WhenNoVenueIdIsPassed() { _venueService.GetFilteredVenues().Returns(TestData.VenueTestData); var result = await _venueController.GetVenues(); var resultValue = ControllerTestHelper.ActionResultToOkResultValue>(result); Assert.NotNull(resultValue); Assert.AreEqual(TestData.VenueTestData.Count(), resultValue.Count()); } [Test] public async Task GetVenue_ReturnsOk_WithVenue_WhenVenueIdIsPassedAndIsReturnedFromService() { var venueTestData = TestData.VenueTestData.ToArray(); _venueService.GetVenue(Arg.Any()).Returns(venueTestData[0]); var result = await _venueController.GetVenue(1); var resultValue = ControllerTestHelper.ActionResultToOkResultValue(result); Assert.AreEqual(venueTestData[0].Id, resultValue.Id); Assert.AreEqual(venueTestData[0].Name, resultValue.Name); } [TestCase(0)] [TestCase(-1)] [TestCase(-1000)] public async Task GetVenue_ReturnsBadRequest_WhenVenueIdPassedIsInvalid(int venueId) { var result = await _venueController.GetVenue(-1); var badRequestResult = result as BadRequestObjectResult; Assert.NotNull(badRequestResult); var badRequestString = badRequestResult.Value as string; Assert.NotNull(badRequestString); Assert.AreEqual("Requested venue ID must be more than zero", badRequestString); } [Test] public async Task GetVenue_ReturnsNotFound_WhenVenueIsNotReturnedFromService() { _venueService.GetVenue(Arg.Any()).ReturnsNull(); var result = await _venueController.GetVenue(1); var notFoundResult = result as NotFoundResult; Assert.NotNull(notFoundResult); } }