1
+ using ECommerceWebApp . Areas . Admin . Controllers ;
2
+ using ECommerceSystem . Models ;
3
+ using ECommerceSystem . Service . Services . IServices ;
4
+ using Microsoft . AspNetCore . Mvc ;
5
+ using Microsoft . AspNetCore . Mvc . ViewFeatures ;
6
+ using Microsoft . AspNetCore . Http ;
7
+ using Moq ;
8
+
9
+
10
+ namespace ECommerceSystem . Test . ControllerTests
11
+ {
12
+ public class CompanyControllerTests
13
+ {
14
+ private readonly Mock < ICompanyService > _mockCompanyService ;
15
+ private readonly CompanyController _controller ;
16
+
17
+ public CompanyControllerTests ( )
18
+ {
19
+ _mockCompanyService = new Mock < ICompanyService > ( ) ;
20
+
21
+ _controller = new CompanyController ( _mockCompanyService . Object ) ;
22
+
23
+ // Setup TempData for the controller
24
+ _controller . TempData = new TempDataDictionary (
25
+ new DefaultHttpContext ( ) ,
26
+ Mock . Of < ITempDataProvider > ( ) ) ;
27
+ }
28
+
29
+ [ Fact ]
30
+ public void Index_ReturnsViewWithCompanyList ( )
31
+ {
32
+ // Arrange
33
+ var expectedCompanies = new List < Company >
34
+ {
35
+ new Company { Id = 1 , Name = "Company A" } ,
36
+ new Company { Id = 2 , Name = "Company B" }
37
+ } ;
38
+ _mockCompanyService . Setup ( s => s . GetAllCompanies ( ) ) . Returns ( expectedCompanies ) ;
39
+
40
+ // Act
41
+ var result = _controller . Index ( ) ;
42
+
43
+ // Assert
44
+ var viewResult = Assert . IsType < ViewResult > ( result ) ;
45
+ var model = Assert . IsAssignableFrom < IEnumerable < Company > > ( viewResult . Model ) ;
46
+ Assert . Equal ( expectedCompanies . Count , model . Count ( ) ) ;
47
+ Assert . Equal ( expectedCompanies , model ) ;
48
+ }
49
+
50
+ [ Fact ]
51
+ public void Create_Get_ReturnsView ( )
52
+ {
53
+ // Act
54
+ var result = _controller . Create ( ) ;
55
+
56
+ // Assert
57
+ Assert . IsType < ViewResult > ( result ) ;
58
+ }
59
+
60
+ [ Fact ]
61
+ public void Create_Post_InvalidModelState_ReturnsViewWithSameModel ( )
62
+ {
63
+ // Arrange
64
+ var company = new Company { Id = 0 , Name = "Test Company" } ;
65
+ _controller . ModelState . AddModelError ( "Error" , "Test error" ) ;
66
+
67
+ // Act
68
+ var result = _controller . Create ( company ) ;
69
+
70
+ // Assert
71
+ var viewResult = Assert . IsType < ViewResult > ( result ) ;
72
+ var model = Assert . IsAssignableFrom < Company > ( viewResult . Model ) ;
73
+ Assert . Equal ( company , model ) ;
74
+ }
75
+
76
+ [ Fact ]
77
+ public void Create_Post_ValidModelState_CreatesCompanyAndRedirects ( )
78
+ {
79
+ // Arrange
80
+ var company = new Company { Id = 0 , Name = "New Company" } ;
81
+
82
+ // Act
83
+ var result = _controller . Create ( company ) ;
84
+
85
+ // Assert
86
+ _mockCompanyService . Verify ( s => s . AddCompany ( company ) , Times . Once ) ;
87
+
88
+ var redirectResult = Assert . IsType < RedirectToActionResult > ( result ) ;
89
+ Assert . Equal ( "Index" , redirectResult . ActionName ) ;
90
+ Assert . Equal ( "Company created successfully" , _controller . TempData [ "success" ] ) ;
91
+ }
92
+
93
+ [ Fact ]
94
+ public void LoadCompanyView_IdIsNullOrZero_ReturnsNotFound ( )
95
+ {
96
+ // Call the private method using reflection
97
+ var method = typeof ( CompanyController ) . GetMethod ( "LoadCompanyView" ,
98
+ System . Reflection . BindingFlags . NonPublic | System . Reflection . BindingFlags . Instance ) ;
99
+
100
+ // Test with null id
101
+ var result1 = method . Invoke ( _controller , new object [ ] { null , "Edit" } ) as IActionResult ;
102
+ Assert . IsType < NotFoundResult > ( result1 ) ;
103
+
104
+ // Test with zero id
105
+ var result2 = method . Invoke ( _controller , new object [ ] { 0 , "Edit" } ) as IActionResult ;
106
+ Assert . IsType < NotFoundResult > ( result2 ) ;
107
+ }
108
+
109
+ [ Fact ]
110
+ public void LoadCompanyView_CompanyNotFound_ReturnsNotFound ( )
111
+ {
112
+ // Arrange
113
+ int companyId = 99 ;
114
+ _mockCompanyService . Setup ( s => s . GetCompanyById ( companyId ) ) . Returns ( ( Company ) null ) ;
115
+
116
+ // Call the private method using reflection
117
+ var method = typeof ( CompanyController ) . GetMethod ( "LoadCompanyView" ,
118
+ System . Reflection . BindingFlags . NonPublic | System . Reflection . BindingFlags . Instance ) ;
119
+
120
+ // Act
121
+ var result = method . Invoke ( _controller , new object [ ] { companyId , "Edit" } ) as IActionResult ;
122
+
123
+ // Assert
124
+ Assert . IsType < NotFoundResult > ( result ) ;
125
+ }
126
+
127
+ [ Fact ]
128
+ public void LoadCompanyView_CompanyFound_ReturnsViewWithCompany ( )
129
+ {
130
+ // Arrange
131
+ int companyId = 1 ;
132
+ var company = new Company { Id = companyId , Name = "Test Company" } ;
133
+
134
+ _mockCompanyService . Setup ( s => s . GetCompanyById ( companyId ) ) . Returns ( company ) ;
135
+
136
+ // Call the private method using reflection
137
+ var method = typeof ( CompanyController ) . GetMethod ( "LoadCompanyView" ,
138
+ System . Reflection . BindingFlags . NonPublic | System . Reflection . BindingFlags . Instance ) ;
139
+
140
+ // Act
141
+ var result = method . Invoke ( _controller , new object [ ] { companyId , "TestView" } ) as IActionResult ;
142
+
143
+ // Assert
144
+ var viewResult = Assert . IsType < ViewResult > ( result ) ;
145
+ Assert . Equal ( "TestView" , viewResult . ViewName ) ;
146
+ Assert . Equal ( company , viewResult . Model ) ;
147
+ }
148
+
149
+ [ Fact ]
150
+ public void Edit_Get_InvalidModelState_ReturnsBadRequest ( )
151
+ {
152
+ // Arrange
153
+ _controller . ModelState . AddModelError ( "Error" , "Test error" ) ;
154
+
155
+ // Act
156
+ var result = _controller . Edit ( 1 ) ;
157
+
158
+ // Assert
159
+ Assert . IsType < BadRequestObjectResult > ( result ) ;
160
+ }
161
+
162
+ [ Fact ]
163
+ public void Edit_Get_ValidModelState_CallsLoadCompanyView ( )
164
+ {
165
+ // Arrange
166
+ int companyId = 1 ;
167
+ var company = new Company { Id = companyId , Name = "Test Company" } ;
168
+
169
+ _mockCompanyService . Setup ( s => s . GetCompanyById ( companyId ) ) . Returns ( company ) ;
170
+
171
+ // Act
172
+ var result = _controller . Edit ( companyId ) ;
173
+
174
+ // Assert
175
+ var viewResult = Assert . IsType < ViewResult > ( result ) ;
176
+ Assert . Equal ( "Edit" , viewResult . ViewName ) ;
177
+ Assert . Equal ( company , viewResult . Model ) ;
178
+ }
179
+
180
+ [ Fact ]
181
+ public void Edit_Post_InvalidModelState_ReturnsViewWithSameModel ( )
182
+ {
183
+ // Arrange
184
+ var company = new Company { Id = 1 , Name = "Updated Company" } ;
185
+ _controller . ModelState . AddModelError ( "Error" , "Test error" ) ;
186
+
187
+ // Act
188
+ var result = _controller . Edit ( company ) ;
189
+
190
+ // Assert
191
+ var viewResult = Assert . IsType < ViewResult > ( result ) ;
192
+ var model = Assert . IsAssignableFrom < Company > ( viewResult . Model ) ;
193
+ Assert . Equal ( company , model ) ;
194
+ }
195
+
196
+ [ Fact ]
197
+ public void Edit_Post_ValidModelState_UpdatesCompanyAndRedirects ( )
198
+ {
199
+ // Arrange
200
+ var company = new Company { Id = 1 , Name = "Updated Company" } ;
201
+
202
+ // Act
203
+ var result = _controller . Edit ( company ) ;
204
+
205
+ // Assert
206
+ _mockCompanyService . Verify ( s => s . UpdateCompany ( company ) , Times . Once ) ;
207
+
208
+ var redirectResult = Assert . IsType < RedirectToActionResult > ( result ) ;
209
+ Assert . Equal ( "Index" , redirectResult . ActionName ) ;
210
+ Assert . Equal ( "Company updated successfully" , _controller . TempData [ "success" ] ) ;
211
+ }
212
+
213
+ [ Fact ]
214
+ public void Delete_Get_InvalidModelState_ReturnsBadRequest ( )
215
+ {
216
+ // Arrange
217
+ _controller . ModelState . AddModelError ( "Error" , "Test error" ) ;
218
+
219
+ // Act
220
+ var result = _controller . Delete ( 1 ) ;
221
+
222
+ // Assert
223
+ Assert . IsType < BadRequestObjectResult > ( result ) ;
224
+ }
225
+
226
+ [ Fact ]
227
+ public void Delete_Get_ValidModelState_CallsLoadCompanyView ( )
228
+ {
229
+ // Arrange
230
+ int companyId = 1 ;
231
+ var company = new Company { Id = companyId , Name = "Test Company" } ;
232
+
233
+ _mockCompanyService . Setup ( s => s . GetCompanyById ( companyId ) ) . Returns ( company ) ;
234
+
235
+ // Act
236
+ var result = _controller . Delete ( companyId ) ;
237
+
238
+ // Assert
239
+ var viewResult = Assert . IsType < ViewResult > ( result ) ;
240
+ Assert . Equal ( "Delete" , viewResult . ViewName ) ;
241
+ Assert . Equal ( company , viewResult . Model ) ;
242
+ }
243
+
244
+ [ Fact ]
245
+ public void DeleteConfirmed_InvalidModelState_ReturnsBadRequest ( )
246
+ {
247
+ // Arrange
248
+ _controller . ModelState . AddModelError ( "Error" , "Test error" ) ;
249
+
250
+ // Act
251
+ var result = _controller . DeleteConfirmed ( 1 ) ;
252
+
253
+ // Assert
254
+ Assert . IsType < BadRequestObjectResult > ( result ) ;
255
+ }
256
+
257
+ [ Fact ]
258
+ public void DeleteConfirmed_IdIsNull_ReturnsNotFound ( )
259
+ {
260
+ // Act
261
+ var result = _controller . DeleteConfirmed ( null ) ;
262
+
263
+ // Assert
264
+ Assert . IsType < NotFoundResult > ( result ) ;
265
+ }
266
+
267
+ [ Fact ]
268
+ public void DeleteConfirmed_ValidIdProvided_DeletesCompanyAndRedirects ( )
269
+ {
270
+ // Arrange
271
+ int companyId = 1 ;
272
+
273
+ // Act
274
+ var result = _controller . DeleteConfirmed ( companyId ) ;
275
+
276
+ // Assert
277
+ _mockCompanyService . Verify ( s => s . DeleteCompany ( companyId ) , Times . Once ) ;
278
+
279
+ var redirectResult = Assert . IsType < RedirectToActionResult > ( result ) ;
280
+ Assert . Equal ( "Index" , redirectResult . ActionName ) ;
281
+ Assert . Equal ( "Company deleted successfully" , _controller . TempData [ "success" ] ) ;
282
+ }
283
+ }
284
+ }
0 commit comments