Skip to content

Commit

Permalink
set TodoEndpoints unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jicking committed Apr 17, 2024
1 parent a24196e commit b0fe895
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
75 changes: 75 additions & 0 deletions JixMinApiTests/Features/Todo/TodoEndpointsTests.cs
Original file line number Diff line number Diff line change
@@ -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<IMediator>();
var emptyId = Guid.Empty;
var expectedBadRequest = TypedResults.BadRequest<ValidationErrorDto>(
new ValidationErrorDto(
[new ValidationErrorItem("id", "id must not be an empty guid.")]
));

// Act
var result = await TodoEndpoints.GetTodoByIdAsync(emptyId, mediatorMock.Object);

// Assert
Assert.IsType<Results<BadRequest<ValidationErrorDto>, NotFound, Ok<TodoDto>>>(result);
var badrequest = (BadRequest<ValidationErrorDto>)result.Result;
Assert.NotNull(badrequest);
}

[Fact]
public async Task GetTodoByIdAsync_Returns_NotFound_When_Todo_Not_Found()
{
// Arrange
var mediatorMock = new Mock<IMediator>();
var nonExistentId = Guid.NewGuid(); // Assuming this id doesn't exist
mediatorMock.Setup(m => m.Send(It.IsAny<GetAllTodosQuery>(), CancellationToken.None))
.ReturnsAsync(new List<TodoDto>());

// Act
var result = await TodoEndpoints.GetTodoByIdAsync(nonExistentId, mediatorMock.Object);

// Assert
Assert.IsType<Results<BadRequest<ValidationErrorDto>, NotFound, Ok<TodoDto>>>(result);
var notFoundResult = (NotFound)result.Result;

Assert.NotNull(notFoundResult);
}

[Fact]
public async Task GetTodoByIdAsync_Returns_Ok_When_Todo_Found()
{
// Arrange
var mediatorMock = new Mock<IMediator>();
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<GetAllTodosQuery>(), CancellationToken.None))
.ReturnsAsync(new List<TodoDto>() {
todoDto
});

// Act
var result = await TodoEndpoints.GetTodoByIdAsync(existingId, mediatorMock.Object);

// Assert
Assert.IsType<Results<BadRequest<ValidationErrorDto>, NotFound, Ok<TodoDto>>>(result);
var okResult = (Ok<TodoDto>)result.Result;

Assert.NotNull(okResult);
Assert.Equal(todoDto, okResult.Value);
}
}
1 change: 1 addition & 0 deletions JixMinApiTests/JixMinApiTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="MSTest.TestAdapter" Version="3.1.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.1.1" />
<PackageReference Include="xunit" Version="2.6.6" />
Expand Down

0 comments on commit b0fe895

Please sign in to comment.