x-lab-lbq-technical-test/backend/LeedsBeerQuest.API/LeedsBeerQuest.API.Tests/Controllers/VenueControllerTests.cs

80 lines
2.5 KiB
C#
Raw Normal View History

2022-10-30 15:35:39 +00:00
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;
2022-10-30 03:41:36 +00:00
using NUnit.Framework;
namespace LeedsBeerQuest.API.Tests.Controllers;
[TestFixture]
public class VenueControllerTests
{
2022-10-30 15:35:39 +00:00
private IVenueService _venueService;
private VenueController _venueController;
[SetUp]
public void SetUp()
{
_venueService = Substitute.For<IVenueService>();
_venueController = new VenueController(_venueService);
}
[Test]
2022-10-30 20:23:48 +00:00
public async Task GetAll_ReturnsOk_WithAllVenues_WhenNoVenueIdIsPassed()
2022-10-30 15:35:39 +00:00
{
_venueService.GetAllVenues().Returns(TestData.VenueTestData);
var result = await _venueController.GetAll();
var resultValue = ControllerTestHelper.ActionResultToOkResultValue<IEnumerable<Venue>>(result);
Assert.NotNull(resultValue);
Assert.AreEqual(TestData.VenueTestData.Count(), resultValue.Count());
2022-10-30 15:35:39 +00:00
}
[Test]
2022-10-30 20:23:48 +00:00
public async Task GetVenue_ReturnsOk_WithVenue_WhenVenueIdIsPassedAndIsReturnedFromService()
2022-10-30 15:35:39 +00:00
{
var venueTestData = TestData.VenueTestData.ToArray();
_venueService.GetVenue(Arg.Any<int>()).Returns(venueTestData[0]);
var result = await _venueController.GetVenue(1);
var resultValue = ControllerTestHelper.ActionResultToOkResultValue<Venue>(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<int>()).ReturnsNull();
var result = await _venueController.GetVenue(1);
var notFoundResult = result as NotFoundResult;
Assert.NotNull(notFoundResult);
}
2022-10-30 03:41:36 +00:00
}