Files
2026-04-20 14:47:24 -05:00

112 lines
3.5 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using LCU.API.Collections.RepoApi.Controllers;
using LCU.API.Collections.RepoApi.Exceptions;
using LCU.API.Collections.RepoApi.Models.Responses;
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 RepoCompanySelectionControllerTest
{
private RepoCompanySelectionController _controller;
private Mock<IRepoCompanySelectionService> _mockService;
private Mock<IHttpContextAccessor> _mockHttpContextAccessor;
public RepoCompanySelectionControllerTest()
{
_mockService = new Mock<IRepoCompanySelectionService>();
_mockHttpContextAccessor = new Mock<IHttpContextAccessor>();
_controller = new RepoCompanySelectionController(
_mockService.Object,
_mockHttpContextAccessor.Object);
}
[Fact]
public async void Get_Ok()
{
// Arrange
_mockService.Setup(service => service.ListAllAsync()).ReturnsAsync(new List<RepoCompanySelection>()
{
new RepoCompanySelection(),
new RepoCompanySelection()
});
// Act
var result = await _controller.Get();
// Assert
Assert.NotNull(result);
var okResult = result as OkObjectResult;
Assert.NotNull(okResult);
Assert.NotNull(okResult.Value);
var repoCompanySelectionList = okResult.Value as IEnumerable<RepoCompanySelection>;
Assert.NotNull(repoCompanySelectionList);
Assert.Equal(2, repoCompanySelectionList.Count());
}
[Fact]
public async void Get_ById_Ok()
{
// Arrange
_mockService.Setup(service => service.GetAsync(It.IsAny<int>())).ReturnsAsync(new RepoCompanySelection()
{
RepoCompanyId = 5
});
// Act
var result = await _controller.Get(42);
// Assert
Assert.NotNull(result);
var okResult = result as OkObjectResult;
Assert.NotNull(okResult);
Assert.NotNull(okResult.Value);
var repoCompanySelection = okResult.Value as RepoCompanySelection;
Assert.NotNull(repoCompanySelection);
Assert.Equal(5, repoCompanySelection.RepoCompanyId);
}
[Fact]
public async void Get_ById_Invalid_ID()
{
// Act
var result = await _controller.Get(0);
// Assert
Assert.NotNull(result);
var badRequestResult = result as BadRequestObjectResult;
Assert.NotNull(badRequestResult);
Assert.Equal("Invalid ID", badRequestResult.Value);
}
[Fact]
public async void Get_ById_Not_Found()
{
// Arrange
_mockService.Setup(service => service.GetAsync(It.IsAny<int>()))
.ThrowsAsync(new NotFoundException("Not Found in Get"));
// Act
var result = await _controller.Get(1);
// Assert
Assert.NotNull(result);
var notFoundRequest = result as NotFoundObjectResult;
Assert.NotNull(notFoundRequest);
Assert.Equal("Not Found in Get", notFoundRequest.Value);
}
}
}