YPS-Beer/backend/YPS.Beer.Tests/Controllers/BeerControllerTests.cs
2023-12-07 00:20:59 +00:00

94 lines
2.7 KiB
C#

using Microsoft.AspNetCore.Mvc;
using NSubstitute;
using NSubstitute.ReturnsExtensions;
using NUnit.Framework;
using YPS.Beer.Controllers;
using YPS.Beer.Services;
namespace YPS.Beer.Tests.Controllers;
public class BeerControllerTests
{
[Test]
public async Task GetBeer_ReturnsNotFound_WhenNoBeerFound()
{
var mockService = Substitute.For<IPunkService>();
mockService.GetBeer(1).ReturnsNull();
var controller = new BeerController(mockService);
var result = await controller.GetBeer(1);
Assert.That(result, Is.TypeOf<NotFoundResult>());
}
[Test]
public async Task GetBeer_ReturnsBeer_WhenBeerFound()
{
var mockService = Substitute.For<IPunkService>();
mockService.GetBeer(1).Returns(new Models.Beer
{
Id = 1,
Name = "Some beer!",
});
var controller = new BeerController(mockService);
var result = await controller.GetBeer(1);
var resultObject = result as OkObjectResult;
var resultValue = resultObject?.Value as Models.Beer;
Assert.That(resultValue, Is.Not.Null);
Assert.That(resultValue.Id, Is.EqualTo(1));
Assert.That(resultValue.Name, Is.EqualTo("Some beer!"));
}
[Test]
public async Task SearchBeer_ReturnsEmptyArray_WhenNoBeerFound()
{
var mockService = Substitute.For<IPunkService>();
mockService.FindBeers("asdf").Returns(Array.Empty<Models.Beer>());
var controller = new BeerController(mockService);
var result = await controller.SearchBeer("asdf");
var resultObject = result as OkObjectResult;
var resultValue = resultObject?.Value as Models.Beer[];
Assert.That(resultValue, Is.Not.Null);
Assert.That(resultValue, Is.Empty);
}
[Test]
public async Task SearchBeer_ReturnsBeers_WhenBeersFound()
{
var mockService = Substitute.For<IPunkService>();
mockService.FindBeers("asdf").Returns(new []
{
new Models.Beer
{
Id = 1,
Name = "First beer!",
},
new Models.Beer
{
Id = 14,
Name = "Another beer!",
},
new Models.Beer
{
Id = 45,
Name = "IPA",
},
});
var controller = new BeerController(mockService);
var result = await controller.SearchBeer("asdf");
var resultObject = result as OkObjectResult;
var resultValue = resultObject?.Value as Models.Beer[];
Assert.That(resultValue, Is.Not.Null);
Assert.That(resultValue, Has.Length.EqualTo(3));
}
}