1
+ using AutoMapper ;
2
+ using Microsoft . AspNetCore . Mvc ;
3
+ using Microsoft . Extensions . Logging ;
4
+ using Moq ;
5
+ using Server . Controllers ;
6
+ using Server . DTOs ;
7
+ using Server . Interfaces ;
8
+ using Server . Models ;
9
+ using Server . Profiles ;
10
+ using System . Collections . Generic ;
11
+ using System . Linq ;
12
+ using Xunit ;
13
+
14
+ namespace Server . Tests
15
+ {
16
+ public class InformationCardsControllerTests
17
+ {
18
+ [ Fact ]
19
+ public void GetInformationCards_WithAListOfCards_ReturnAListOfCards ( )
20
+ {
21
+ // Arrange
22
+
23
+ var repository = GenerateMockRepository ( ) ;
24
+
25
+ repository . Setup ( repo => repo . GetAllInformationCards ( ) ) . Returns ( GetTestCards ( ) ) ;
26
+
27
+ var mapper = GenerateMockMapper ( ) ;
28
+ var controller = GenerateMockController ( repository , mapper ) ;
29
+
30
+ // Act
31
+ var result = controller . GetInformationCards ( ) ;
32
+
33
+ // Assert
34
+
35
+ // 1. Checking output result for Not Null
36
+ Assert . NotNull ( result ) ;
37
+
38
+ // 2. Checking output result for output object type as ActionResult<IEnumerable<InformationCardReadDto>>
39
+ var actionResult = Assert . IsType < ActionResult < IEnumerable < InformationCardReadDto > > > ( result ) ;
40
+
41
+ // 3. Checking output result for object type as OkObjectResult (200 OK)
42
+ var model = Assert . IsAssignableFrom < OkObjectResult > ( actionResult . Result ) ;
43
+
44
+ // 4. Checking if count of the returned objects equal to the real objects count
45
+ var modelAsAList = model . Value as List < InformationCardReadDto > ;
46
+ Assert . Equal ( GetTestCards ( ) . Count , modelAsAList . Count ) ;
47
+ }
48
+
49
+ [ Fact ]
50
+ public void GetInformationCardById_WithAnExistingId_ReturnItemWithPassedIndex ( )
51
+ {
52
+ // Arrange
53
+ var repository = GenerateMockRepository ( ) ;
54
+ repository . Setup ( repo => repo . GetInformationCardById ( 1 ) ) . Returns ( GetTestCards ( ) . FirstOrDefault ( e => e . Id == 1 ) ) ;
55
+
56
+ var mapper = GenerateMockMapper ( ) ;
57
+ var controller = GenerateMockController ( repository , mapper ) ;
58
+
59
+ // Act
60
+ var result = controller . GetInformationCardById ( 1 ) ;
61
+
62
+ // Assert
63
+
64
+ // 1. Checking output result for object type as OkObjectResult (200 OK)
65
+ var actionResult = Assert . IsAssignableFrom < OkObjectResult > ( result . Result ) ;
66
+
67
+ // 2. Checking if data is right
68
+ var model = Assert . IsType < InformationCardReadDto > ( actionResult . Value ) ;
69
+ Assert . Equal ( 1 , model . Id ) ;
70
+ Assert . Equal ( "Dog" , model . Name ) ;
71
+ Assert . Equal ( "../../../Data/Images/Dog.jpg" , model . Image ) ;
72
+ }
73
+
74
+ [ Fact ]
75
+ public void GetInformationCardById_WithNotExistingId_ReturnNotFound ( )
76
+ {
77
+ // Arrange
78
+ var repository = GenerateMockRepository ( ) ;
79
+ repository . Setup ( repo => repo . GetInformationCardById ( 10 ) ) . Returns ( GetTestCards ( ) . FirstOrDefault ( e => e . Id == 10 ) ) ;
80
+
81
+ var mapper = GenerateMockMapper ( ) ;
82
+ var controller = GenerateMockController ( repository , mapper ) ;
83
+
84
+ // Act
85
+ var result = controller . GetInformationCardById ( 10 ) ;
86
+
87
+ // Assert
88
+
89
+ // 1. Checking output result for object type as NotFoundObjectResult (404 Not Found)
90
+ var actionResult = Assert . IsAssignableFrom < NotFoundObjectResult > ( result . Result ) ;
91
+
92
+ // 2. Checking if data is right
93
+ var model = Assert . IsType < InformationCardReadDto > ( actionResult . Value ) ;
94
+ Assert . Equal ( null , model ) ;
95
+ }
96
+
97
+
98
+
99
+
100
+
101
+
102
+ private List < InformationCard > GetTestCards ( )
103
+ {
104
+ var cards = new List < InformationCard > ( )
105
+ {
106
+ new InformationCard
107
+ {
108
+ Id = 0 ,
109
+ Name = "Cat" ,
110
+ Image = "../../../Data/Images/Cat.png"
111
+ } ,
112
+ new InformationCard
113
+ {
114
+ Id = 1 ,
115
+ Name = "Dog" ,
116
+ Image = "../../../Data/Images/Dog.jpg"
117
+ } ,
118
+ new InformationCard
119
+ {
120
+ Id = 2 ,
121
+ Name = "Shark" ,
122
+ Image = "../../../Data/Images/Shark.png"
123
+ } ,
124
+ new InformationCard
125
+ {
126
+ Id = 3 ,
127
+ Name = "Ant" ,
128
+ Image = "../../../Data/Images/Ant.jpg"
129
+ } ,
130
+ new InformationCard
131
+ {
132
+ Id = 4 ,
133
+ Name = "Squirrel" ,
134
+ Image = "../../../Data/Images/Squirrel.png"
135
+ }
136
+ } ;
137
+
138
+ return cards ;
139
+ }
140
+
141
+ private Mock < IInformationCardRepo > GenerateMockRepository ( )
142
+ {
143
+ return new Mock < IInformationCardRepo > ( ) ;
144
+ }
145
+ private IMapper GenerateMockMapper ( )
146
+ {
147
+ var mockMapper = new MapperConfiguration ( config =>
148
+ {
149
+ config . AddProfile ( new InformationCardProfile ( ) ) ;
150
+ } ) ;
151
+
152
+ return mockMapper . CreateMapper ( ) ;
153
+ }
154
+ private InformationCardsController GenerateMockController ( Mock < IInformationCardRepo > mockRepository , IMapper mapper )
155
+ {
156
+ return new InformationCardsController ( mockRepository . Object , mapper ) ;
157
+ }
158
+ }
159
+ }
0 commit comments