Add VenueController
tests
This commit is contained in:
parent
51387e770e
commit
470669f3f5
@ -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<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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user