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
+ }
0 commit comments