From b54c1aa5c233c83e75641db8d0aa34fd75ea55fc Mon Sep 17 00:00:00 2001 From: etudiant Date: Mon, 6 Feb 2023 07:21:47 +0100 Subject: [PATCH] les tests parti modifier --- .../Controllers/PartieController.cs | 2 +- .../GraphQL Project/GraphQL Project.csproj | 1 + .../Properties/launchSettings.json | 2 - .../BowlingAPITest/TestPartiControlleur.cs | 192 ++++++++++++++++++ 4 files changed, 194 insertions(+), 3 deletions(-) create mode 100644 Sources/Tests/BowlingAPITest/TestPartiControlleur.cs diff --git a/Sources/BowlingApi/Controllers/PartieController.cs b/Sources/BowlingApi/Controllers/PartieController.cs index cd67275..fc7e7a6 100644 --- a/Sources/BowlingApi/Controllers/PartieController.cs +++ b/Sources/BowlingApi/Controllers/PartieController.cs @@ -105,7 +105,7 @@ namespace BowlingApi.Controllers // PUT: api/Partie/5 [HttpPut("{id}")] - public async Task> Put(string name, [FromBody] PartieDTO partie) + public async Task> Put(long id, [FromBody] PartieDTO partie) { try { diff --git a/Sources/GraphQL Project/GraphQL Project.csproj b/Sources/GraphQL Project/GraphQL Project.csproj index acdda21..eaa97d7 100644 --- a/Sources/GraphQL Project/GraphQL Project.csproj +++ b/Sources/GraphQL Project/GraphQL Project.csproj @@ -7,6 +7,7 @@ GraphQL_Project + diff --git a/Sources/GraphQL Project/Properties/launchSettings.json b/Sources/GraphQL Project/Properties/launchSettings.json index fedf6f9..8730302 100644 --- a/Sources/GraphQL Project/Properties/launchSettings.json +++ b/Sources/GraphQL Project/Properties/launchSettings.json @@ -18,8 +18,6 @@ }, "GraphQL": { "commandName": "Project", - "dotnetRunMessages": true, - "launchBrowser": false, "applicationUrl": "https://localhost:5002", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" diff --git a/Sources/Tests/BowlingAPITest/TestPartiControlleur.cs b/Sources/Tests/BowlingAPITest/TestPartiControlleur.cs new file mode 100644 index 0000000..ff24615 --- /dev/null +++ b/Sources/Tests/BowlingAPITest/TestPartiControlleur.cs @@ -0,0 +1,192 @@ +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_id_Should_Return_BadRequest() + //{ + // // Arrange + // var PartieController = new PartieController(null); + + // // Act + // var result = await PartieController.Get(0); + + // // Assert + // result.Should().BeOfType(); + // var badRequestResult = result as BadRequestObjectResult; + // badRequestResult.Value.Should().Be("Le id de la partie est obligatoire"); + //} + + [Fact] + public async Task Get_With_Valid_id_Should_Return_Ok_With_partie() + { + // Arrange + var parti = new PartieDTO { Id = 1, Score = 1 }; + var partiservicemock = new Mock(); + partiservicemock.Setup(x => x.GetDataWithId(1)).ReturnsAsync(parti); + var particontrolleur = new PartieController(partiservicemock.Object); + + // Act + var result = await particontrolleur.Get(1); + + // Assert + result.Should().BeOfType(); + var okResult = result as OkObjectResult; + okResult.Value.Should().BeEquivalentTo(parti); + } + + //post + + + + [Fact] + public async Task Put_With_Valid_parti_Should_Return_Ok_With_parti() + { + // Arrange + var parti = new PartieDTO { Id = 1, Score = 1 }; + var partiServiceMock = new Mock(); + partiServiceMock.Setup(x => x.Update(parti)).ReturnsAsync(true); + var partiControlleur = new PartieController(partiServiceMock.Object); + + // Act + var result = await partiControlleur.Put(parti.Id, parti); + + // Assert + result.Should().BeOfType>(); + var actionResult = result as ActionResult; + actionResult.Result.Should().BeOfType(); + } + + //test Get_ShouldReturnNotFound + [Fact] + public async Task Get_ShouldReturnNotFound() + { + // Arrange + var mockService = new Mock(); + mockService.Setup(service => service.GetAll()).ReturnsAsync((List)null); + var controller = new PartieController(mockService.Object); + + // Act + var result = await controller.Get(); + + // Assert + result.Should().BeOfType(); + } + + //[Fact] + //public async Task Get_White_id_ShouldReturnNotFound() + //{ + // // Arrange + + // var partie1 = new PartieDTO { Score = 1 }; + // var mockService = new Mock(); + // mockService.Setup(service => service.GetDataWithId(2)).ReturnsAsync(partie1); + // var controller = new PartieController(mockService.Object); + + // // Act + // var result = await controller.Get(2); + + // // Assert + // result.Should().BeOfType(); + //} + [Fact] + public async Task Get_ShouldReturnInternalServerError() + { + // Arrange + var mockService = new Mock(); + mockService.Setup(service => service.GetAll()).ThrowsAsync(new Exception()); + var controller = new PartieController(mockService.Object); + + // Act + var result = await controller.Get() as ObjectResult; + + // Assert + result.Should().BeOfType(); + result.StatusCode.Should().Be(500); + } + //TEST POST + //[Fact] + //public async Task Post_With_Invalid_parti_Should_Return_BadRequest() + //{ + // // Arrange + // var PartieController = new PartieController(null); + + // // Act + // var result = await PartieController.Post(null); + + // // Assert + // result.Should().BeOfType>(); + // var actionResult = result as ActionResult; + // actionResult.Result.Should().BeOfType(); + // var badRequestResult = actionResult.Result as BadRequestObjectResult; + // badRequestResult.Value.Should().Be("La partie est obligatoire"); + //} + + + [Fact] + void test() + { + Assert.Equal(1, 1); + + } + + + + + + + } + + +}