105 lines
3.3 KiB
C#
105 lines
3.3 KiB
C#
using System.Collections.Generic;
|
|
using LCU.API.Collections.RepoApi.Controllers;
|
|
using LCU.API.Collections.RepoApi.Services;
|
|
using LCU.Shared.Collections.RepoApiLib.Models;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Moq;
|
|
using Xunit;
|
|
|
|
namespace LCU.API.Collections.RepoApiTests.Controllers
|
|
{
|
|
public class AuctionHouseControllerTest
|
|
{
|
|
private readonly Mock<IAuctionHouseService> mockAuctionHouseService;
|
|
private readonly AuctionHouseController controller;
|
|
private readonly Mock<IHttpContextAccessor> mockHttpContextAccessor;
|
|
|
|
public AuctionHouseControllerTest()
|
|
{
|
|
mockAuctionHouseService = new Mock<IAuctionHouseService>();
|
|
mockHttpContextAccessor = new Mock<IHttpContextAccessor>();
|
|
controller = new AuctionHouseController(mockAuctionHouseService.Object,
|
|
mockHttpContextAccessor.Object);
|
|
}
|
|
|
|
[Fact]
|
|
public async void Get_OK()
|
|
{
|
|
// arrange
|
|
mockAuctionHouseService.Setup(service => service.ListAllAsync()).ReturnsAsync(new List<AuctionHouse>()
|
|
{
|
|
new AuctionHouse()
|
|
{
|
|
Name = "Auction House A"
|
|
}
|
|
});
|
|
|
|
// act
|
|
var result = await controller.Get();
|
|
Assert.NotNull(result);
|
|
var okResult = result as OkObjectResult;
|
|
|
|
// assert
|
|
Assert.NotNull(okResult);
|
|
Assert.Equal(200, okResult.StatusCode);
|
|
|
|
var okValue = okResult.Value as IEnumerable<AuctionHouse>;
|
|
Assert.NotNull(okValue);
|
|
|
|
var enumerator = okValue.GetEnumerator();
|
|
enumerator.MoveNext();
|
|
Assert.NotNull(enumerator.Current);
|
|
Assert.Equal("Auction House A", enumerator.Current.Name);
|
|
}
|
|
|
|
[Fact]
|
|
public async void Post_OK()
|
|
{
|
|
// arrange
|
|
mockAuctionHouseService.Setup(service => service.SaveAsync(It.IsAny<AuctionHouse>())).ReturnsAsync(
|
|
new AuctionHouse()
|
|
{
|
|
Name = "Auction House A"
|
|
});
|
|
|
|
// act
|
|
var result = await controller.Post(new AuctionHouse()
|
|
{
|
|
Name = "Auction House A"
|
|
});
|
|
Assert.NotNull(result);
|
|
var okResult = result as OkObjectResult;
|
|
|
|
// assert
|
|
Assert.NotNull(okResult);
|
|
Assert.Equal(200, okResult.StatusCode);
|
|
|
|
var okValue = okResult.Value as AuctionHouse;
|
|
Assert.NotNull(okValue);
|
|
Assert.Equal("Auction House A", okValue.Name);
|
|
}
|
|
|
|
[Fact]
|
|
public async void Put_Bad_Request()
|
|
{
|
|
// arrange
|
|
mockAuctionHouseService.Setup(service => service.UpdateAsync(It.IsAny<AuctionHouse>()))
|
|
.ReturnsAsync(new AuctionHouse());
|
|
|
|
// act
|
|
var result = await controller.Put("ID1", new AuctionHouse()
|
|
{
|
|
AuctionID = "ID2"
|
|
});
|
|
|
|
|
|
// assert
|
|
Assert.NotNull(result);
|
|
var badRequestResult = result as BadRequestObjectResult;
|
|
|
|
Assert.NotNull(badRequestResult);
|
|
Assert.Equal(StatusCodes.Status400BadRequest, badRequestResult.StatusCode);
|
|
}
|
|
}
|
|
} |