From b3b9091c684b43d44390bc7c92b2b1ebdcad721d Mon Sep 17 00:00:00 2001 From: jicking <7297029+jicking@users.noreply.github.com> Date: Wed, 17 Apr 2024 17:02:22 +0800 Subject: [PATCH] set TodoEndpoints unit tests --- .../Commands/CreateTodoCommandHandlerTests.cs | 12 +-- .../Queries/GetAllTodosQueryHandlerTests.cs | 6 +- .../Features/Todo/TodoEndpointsTests.cs | 75 +++++++++++++++++++ JixMinApiTests/JixMinApiTests.csproj | 1 + 4 files changed, 85 insertions(+), 9 deletions(-) create mode 100644 JixMinApiTests/Features/Todo/TodoEndpointsTests.cs diff --git a/JixMinApiTests/Features/Todo/Commands/CreateTodoCommandHandlerTests.cs b/JixMinApiTests/Features/Todo/Commands/CreateTodoCommandHandlerTests.cs index af203f4..991ecea 100644 --- a/JixMinApiTests/Features/Todo/Commands/CreateTodoCommandHandlerTests.cs +++ b/JixMinApiTests/Features/Todo/Commands/CreateTodoCommandHandlerTests.cs @@ -1,6 +1,6 @@ using JixMinApiTests.Features.Todo; using Xunit; -using Assert = Microsoft.VisualStudio.TestTools.UnitTesting.Assert; +using Assert = Xunit.Assert; namespace JixMinApi.Features.Todo.Commands.Tests; @@ -18,7 +18,7 @@ internal void Setup() public void CreateTodoCommandHandlerTest() { Setup(); - Assert.IsNotNull(sut); + Assert.NotNull(sut); } [Fact()] @@ -28,9 +28,9 @@ public async void HandleTest() var input = new TodoCreateDto("Test", true); var result = await sut.Handle(new CreateTodoCommand(input), default); - Xunit.Assert.NotNull(result); - Assert.IsTrue(result.IsSuccess); - Assert.AreEqual(input.Name, result.Value.Name); - Assert.AreEqual(input.IsComplete, result.Value.IsComplete); + Assert.NotNull(result); + Assert.True(result.IsSuccess); + Assert.Equal(input.Name, result.Value.Name); + Assert.Equal(input.IsComplete, result.Value.IsComplete); } } \ No newline at end of file diff --git a/JixMinApiTests/Features/Todo/Queries/GetAllTodosQueryHandlerTests.cs b/JixMinApiTests/Features/Todo/Queries/GetAllTodosQueryHandlerTests.cs index 9c2278c..010e237 100644 --- a/JixMinApiTests/Features/Todo/Queries/GetAllTodosQueryHandlerTests.cs +++ b/JixMinApiTests/Features/Todo/Queries/GetAllTodosQueryHandlerTests.cs @@ -1,6 +1,6 @@ using JixMinApiTests.Features.Todo; using Xunit; -using Assert = Microsoft.VisualStudio.TestTools.UnitTesting.Assert; +using Assert = Xunit.Assert; namespace JixMinApi.Features.Todo.Queries.Tests; @@ -18,7 +18,7 @@ internal void Setup() public void GetAllTodosQueryHandlerTest() { Setup(); - Assert.IsNotNull(sut); + Assert.NotNull(sut); } [Fact()] @@ -26,6 +26,6 @@ public async void HandleTest() { Setup(); var result = await sut.Handle(new GetAllTodosQuery(), default); - Xunit.Assert.NotEmpty(result); + Assert.NotEmpty(result); } } \ No newline at end of file diff --git a/JixMinApiTests/Features/Todo/TodoEndpointsTests.cs b/JixMinApiTests/Features/Todo/TodoEndpointsTests.cs new file mode 100644 index 0000000..144e60a --- /dev/null +++ b/JixMinApiTests/Features/Todo/TodoEndpointsTests.cs @@ -0,0 +1,75 @@ +using JixMinApi.Features.Todo.Queries; +using MediatR; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Http.HttpResults; +using Moq; +using Xunit; +using Assert = Xunit.Assert; + +namespace JixMinApi.Features.Todo.Tests; + +public class TodoEndpointsTests +{ + [Fact] + public async Task GetTodoByIdAsync_Returns_BadRequest_When_Id_Is_Empty() + { + // Arrange + var mediatorMock = new Mock(); + var emptyId = Guid.Empty; + var expectedBadRequest = TypedResults.BadRequest( + new ValidationErrorDto( + [new ValidationErrorItem("id", "id must not be an empty guid.")] + )); + + // Act + var result = await TodoEndpoints.GetTodoByIdAsync(emptyId, mediatorMock.Object); + + // Assert + Assert.IsType, NotFound, Ok>>(result); + var badrequest = (BadRequest)result.Result; + Assert.NotNull(badrequest); + } + + [Fact] + public async Task GetTodoByIdAsync_Returns_NotFound_When_Todo_Not_Found() + { + // Arrange + var mediatorMock = new Mock(); + var nonExistentId = Guid.NewGuid(); // Assuming this id doesn't exist + mediatorMock.Setup(m => m.Send(It.IsAny(), CancellationToken.None)) + .ReturnsAsync(new List()); + + // Act + var result = await TodoEndpoints.GetTodoByIdAsync(nonExistentId, mediatorMock.Object); + + // Assert + Assert.IsType, NotFound, Ok>>(result); + var notFoundResult = (NotFound)result.Result; + + Assert.NotNull(notFoundResult); + } + + [Fact] + public async Task GetTodoByIdAsync_Returns_Ok_When_Todo_Found() + { + // Arrange + var mediatorMock = new Mock(); + var existingId = Guid.NewGuid(); // Assuming this id exists + var todoDto = new TodoDto(existingId, "", false); // Assuming todo with this id exists + + mediatorMock.Setup(m => m.Send(It.IsAny(), CancellationToken.None)) + .ReturnsAsync(new List() { + todoDto + }); + + // Act + var result = await TodoEndpoints.GetTodoByIdAsync(existingId, mediatorMock.Object); + + // Assert + Assert.IsType, NotFound, Ok>>(result); + var okResult = (Ok)result.Result; + + Assert.NotNull(okResult); + Assert.Equal(todoDto, okResult.Value); + } +} \ No newline at end of file diff --git a/JixMinApiTests/JixMinApiTests.csproj b/JixMinApiTests/JixMinApiTests.csproj index 0a61e96..c4a3aa8 100644 --- a/JixMinApiTests/JixMinApiTests.csproj +++ b/JixMinApiTests/JixMinApiTests.csproj @@ -12,6 +12,7 @@ +