80 lines
2.5 KiB
C#
80 lines
2.5 KiB
C#
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<IVenueService>();
|
|
_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<IEnumerable<Venue>>(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<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);
|
|
}
|
|
} |