diff --git a/Sources/Tests/BowlingAPITest/BowlingAPITest.csproj b/Sources/Tests/BowlingAPITest/BowlingAPITest.csproj index 0a87957..125c01c 100644 --- a/Sources/Tests/BowlingAPITest/BowlingAPITest.csproj +++ b/Sources/Tests/BowlingAPITest/BowlingAPITest.csproj @@ -22,11 +22,13 @@ runtime; build; native; contentfiles; analyzers; buildtransitive all + + @@ -35,4 +37,7 @@ + + + diff --git a/Sources/Tests/BowlingAPITest/TestPartiControlleur.cs b/Sources/Tests/BowlingAPITest/TestPartiControlleur.cs new file mode 100644 index 0000000..7847146 --- /dev/null +++ b/Sources/Tests/BowlingAPITest/TestPartiControlleur.cs @@ -0,0 +1,93 @@ +using DTOs; +using FluentAssertions; + +namespace BowlingAPITest +{ + public class TestPartiControlleur + { + [Fact] + public async void Get_ShouldReturnOkResult() + { + // Arrange + var partie1 = new PartieDTO { Score=1 }; + var partie2 = new PartieDTO { Score = 2 }; + var parti = GetTestItems(); + var mockService = new Mock(); + mockService.Setup(service => service.GetAll()).ReturnsAsync(parti); + var controller = new PartieController(mockService.Object); + + // Act + var result = await controller.Get() as OkObjectResult; + var value = result.Value as List; + + // Assert + result.Should().NotBeNull(); + value.Should().NotBeNull(); + result.StatusCode.Should().Be(200); + value.Should().BeEquivalentTo(parti); + + } + [Fact] + public async void Get_ShouldReturnAllItems() + { + // Arrange + var testItems = GetTestItems(); + var mockService = new Mock(); + mockService.Setup(service => service.GetAll()).ReturnsAsync(testItems); + var controller = new PartieController(mockService.Object); + + // Act + var result = await controller.Get(); + + // Assert + var okResult = result as OkObjectResult; + var items = Assert.IsType>(okResult.Value); + Assert.Equal(2, items.Count); + } + + private IEnumerable GetTestItems() + { + + var testItems = new List(); + testItems.Add(new PartieDTO { Score = 1 }); + testItems.Add(new PartieDTO { Score = 2 }); + return testItems; + } + [Fact] + public async Task Get_With_Invalid_Name_Should_Return_BadRequest() + { + // Arrange + var PartieController = new PartieController(null); + + // Act + var result = await PartieController.Get(null); + + // Assert + result.Should().BeOfType(); + var badRequestResult = result as BadRequestObjectResult; + badRequestResult.Value.Should().Be("Le score de la partie est obligatoire"); + } + + [Fact] + public async Task Get_With_Valid_Name_Should_Return_Ok_With_partie() + { + // Arrange + var parti = new PartieDTO { Id = 1, Score = 1 }; + var partiservicemock = new Mock(); + partiservicemock.Setup(x => x.GetDataWithName("John Doe")).ReturnsAsync(parti); + var particontrolleur = new PartieController(partiservicemock.Object); + + // Act + var result = await particontrolleur.Get("John Doe"); + + // Assert + result.Should().BeOfType(); + var okResult = result as OkObjectResult; + okResult.Value.Should().BeEquivalentTo(parti); + } + + } + + +} +