From 470669f3f57917f3713a420fe55f892182c841d1 Mon Sep 17 00:00:00 2001 From: Stedoss <29103029+Stedoss@users.noreply.github.com> Date: Sun, 30 Oct 2022 15:35:39 +0000 Subject: [PATCH] Add `VenueController` tests --- .../Controllers/VenueControllerTests.cs | 73 ++++++++++++++++++- 1 file changed, 72 insertions(+), 1 deletion(-) diff --git a/backend/LeedsBeerQuest.API/LeedsBeerQuest.API.Tests/Controllers/VenueControllerTests.cs b/backend/LeedsBeerQuest.API/LeedsBeerQuest.API.Tests/Controllers/VenueControllerTests.cs index c102d36..e3d5c32 100644 --- a/backend/LeedsBeerQuest.API/LeedsBeerQuest.API.Tests/Controllers/VenueControllerTests.cs +++ b/backend/LeedsBeerQuest.API/LeedsBeerQuest.API.Tests/Controllers/VenueControllerTests.cs @@ -1,3 +1,14 @@ +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; @@ -5,5 +16,65 @@ 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_ReturnsAllVenues_WhenNoVenueIdIsPassed() + { + _venueService.GetAllVenues().Returns(TestData.VenueTestData); + + var result = await _venueController.GetAll(); + var resultValue = ControllerTestHelper.ActionResultToOkResultValue>(result); + + Assert.NotNull(resultValue); + Assert.AreEqual(1, resultValue.Count()); + } + + [Test] + public async Task GetVenue_ReturnsVenue_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); + } } \ No newline at end of file