Skip to content

Commit b3b9091

Browse files
committed
set TodoEndpoints unit tests
1 parent a24196e commit b3b9091

File tree

4 files changed

+85
-9
lines changed

4 files changed

+85
-9
lines changed

JixMinApiTests/Features/Todo/Commands/CreateTodoCommandHandlerTests.cs

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using JixMinApiTests.Features.Todo;
22
using Xunit;
3-
using Assert = Microsoft.VisualStudio.TestTools.UnitTesting.Assert;
3+
using Assert = Xunit.Assert;
44

55
namespace JixMinApi.Features.Todo.Commands.Tests;
66

@@ -18,7 +18,7 @@ internal void Setup()
1818
public void CreateTodoCommandHandlerTest()
1919
{
2020
Setup();
21-
Assert.IsNotNull(sut);
21+
Assert.NotNull(sut);
2222
}
2323

2424
[Fact()]
@@ -28,9 +28,9 @@ public async void HandleTest()
2828
var input = new TodoCreateDto("Test", true);
2929
var result = await sut.Handle(new CreateTodoCommand(input), default);
3030

31-
Xunit.Assert.NotNull(result);
32-
Assert.IsTrue(result.IsSuccess);
33-
Assert.AreEqual(input.Name, result.Value.Name);
34-
Assert.AreEqual(input.IsComplete, result.Value.IsComplete);
31+
Assert.NotNull(result);
32+
Assert.True(result.IsSuccess);
33+
Assert.Equal(input.Name, result.Value.Name);
34+
Assert.Equal(input.IsComplete, result.Value.IsComplete);
3535
}
3636
}

JixMinApiTests/Features/Todo/Queries/GetAllTodosQueryHandlerTests.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using JixMinApiTests.Features.Todo;
22
using Xunit;
3-
using Assert = Microsoft.VisualStudio.TestTools.UnitTesting.Assert;
3+
using Assert = Xunit.Assert;
44

55
namespace JixMinApi.Features.Todo.Queries.Tests;
66

@@ -18,14 +18,14 @@ internal void Setup()
1818
public void GetAllTodosQueryHandlerTest()
1919
{
2020
Setup();
21-
Assert.IsNotNull(sut);
21+
Assert.NotNull(sut);
2222
}
2323

2424
[Fact()]
2525
public async void HandleTest()
2626
{
2727
Setup();
2828
var result = await sut.Handle(new GetAllTodosQuery(), default);
29-
Xunit.Assert.NotEmpty(result);
29+
Assert.NotEmpty(result);
3030
}
3131
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
using JixMinApi.Features.Todo.Queries;
2+
using MediatR;
3+
using Microsoft.AspNetCore.Http;
4+
using Microsoft.AspNetCore.Http.HttpResults;
5+
using Moq;
6+
using Xunit;
7+
using Assert = Xunit.Assert;
8+
9+
namespace JixMinApi.Features.Todo.Tests;
10+
11+
public class TodoEndpointsTests
12+
{
13+
[Fact]
14+
public async Task GetTodoByIdAsync_Returns_BadRequest_When_Id_Is_Empty()
15+
{
16+
// Arrange
17+
var mediatorMock = new Mock<IMediator>();
18+
var emptyId = Guid.Empty;
19+
var expectedBadRequest = TypedResults.BadRequest<ValidationErrorDto>(
20+
new ValidationErrorDto(
21+
[new ValidationErrorItem("id", "id must not be an empty guid.")]
22+
));
23+
24+
// Act
25+
var result = await TodoEndpoints.GetTodoByIdAsync(emptyId, mediatorMock.Object);
26+
27+
// Assert
28+
Assert.IsType<Results<BadRequest<ValidationErrorDto>, NotFound, Ok<TodoDto>>>(result);
29+
var badrequest = (BadRequest<ValidationErrorDto>)result.Result;
30+
Assert.NotNull(badrequest);
31+
}
32+
33+
[Fact]
34+
public async Task GetTodoByIdAsync_Returns_NotFound_When_Todo_Not_Found()
35+
{
36+
// Arrange
37+
var mediatorMock = new Mock<IMediator>();
38+
var nonExistentId = Guid.NewGuid(); // Assuming this id doesn't exist
39+
mediatorMock.Setup(m => m.Send(It.IsAny<GetAllTodosQuery>(), CancellationToken.None))
40+
.ReturnsAsync(new List<TodoDto>());
41+
42+
// Act
43+
var result = await TodoEndpoints.GetTodoByIdAsync(nonExistentId, mediatorMock.Object);
44+
45+
// Assert
46+
Assert.IsType<Results<BadRequest<ValidationErrorDto>, NotFound, Ok<TodoDto>>>(result);
47+
var notFoundResult = (NotFound)result.Result;
48+
49+
Assert.NotNull(notFoundResult);
50+
}
51+
52+
[Fact]
53+
public async Task GetTodoByIdAsync_Returns_Ok_When_Todo_Found()
54+
{
55+
// Arrange
56+
var mediatorMock = new Mock<IMediator>();
57+
var existingId = Guid.NewGuid(); // Assuming this id exists
58+
var todoDto = new TodoDto(existingId, "", false); // Assuming todo with this id exists
59+
60+
mediatorMock.Setup(m => m.Send(It.IsAny<GetAllTodosQuery>(), CancellationToken.None))
61+
.ReturnsAsync(new List<TodoDto>() {
62+
todoDto
63+
});
64+
65+
// Act
66+
var result = await TodoEndpoints.GetTodoByIdAsync(existingId, mediatorMock.Object);
67+
68+
// Assert
69+
Assert.IsType<Results<BadRequest<ValidationErrorDto>, NotFound, Ok<TodoDto>>>(result);
70+
var okResult = (Ok<TodoDto>)result.Result;
71+
72+
Assert.NotNull(okResult);
73+
Assert.Equal(todoDto, okResult.Value);
74+
}
75+
}

JixMinApiTests/JixMinApiTests.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<ItemGroup>
1313
<PackageReference Include="coverlet.collector" Version="6.0.0" />
1414
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
15+
<PackageReference Include="Moq" Version="4.20.70" />
1516
<PackageReference Include="MSTest.TestAdapter" Version="3.1.1" />
1617
<PackageReference Include="MSTest.TestFramework" Version="3.1.1" />
1718
<PackageReference Include="xunit" Version="2.6.6" />

0 commit comments

Comments
 (0)