1
1
namespace MyTested . AspNetCore . Mvc . Test . BuildersTests . ActionsTests . ShouldReturnTests
2
2
{
3
3
using Exceptions ;
4
+ using Microsoft . AspNetCore . Mvc ;
5
+ using MyTested . AspNetCore . Mvc . Test . Setups . Models ;
4
6
using Setups ;
5
7
using Setups . Controllers ;
8
+ using System . Collections . Generic ;
6
9
using Xunit ;
7
10
8
11
public class ShouldReturnViewTests
@@ -17,7 +20,47 @@ public void ShouldReturnViewWithNameShouldNotThrowExceptionWithCorrectName()
17
20
. View ( view => view
18
21
. WithName ( "Index" ) ) ;
19
22
}
20
-
23
+
24
+ [ Fact ]
25
+ public void ShouldReturnViewWithNameShouldNotThrowExceptionAndPassAssertions ( )
26
+ {
27
+ MyController < MvcController >
28
+ . Instance ( )
29
+ . Calling ( c => c . IndexView ( ) )
30
+ . ShouldReturn ( )
31
+ . View ( view => view
32
+ . Passing ( ( v ) =>
33
+ {
34
+ Assert . False ( string . IsNullOrEmpty ( v . ViewName ) ) ;
35
+ Assert . Equal ( "Index" , v . ViewName ) ;
36
+ Assert . True ( typeof ( ViewResult ) == v . GetType ( ) ) ;
37
+ } ) ) ;
38
+ }
39
+
40
+ [ Fact ]
41
+ public void ShouldReturnViewShouldNotThrowExceptionWithCorrectViewName ( )
42
+ {
43
+ MyController < MvcController >
44
+ . Instance ( )
45
+ . Calling ( c => c . ViewResultByName ( ) )
46
+ . ShouldReturn ( )
47
+ . View ( view => view
48
+ . WithName ( "TestView" ) ) ;
49
+ }
50
+
51
+ [ Fact ]
52
+ public void ShouldReturnViewWithNameShouldNotThrowExceptionWithCorrectViewResultType ( )
53
+ {
54
+ MyController < MvcController >
55
+ . Instance ( )
56
+ . Calling ( c => c . IndexView ( ) )
57
+ . ShouldReturn ( )
58
+ . View ( view => view
59
+ . WithName ( "Index" )
60
+ . AndAlso ( )
61
+ . Passing ( v => v . GetType ( ) == typeof ( ViewResult ) ) ) ;
62
+ }
63
+
21
64
[ Fact ]
22
65
public void ShouldReturnViewShouldThrowExceptionIfActionResultIsViewResultWithDifferentName ( )
23
66
{
@@ -33,7 +76,39 @@ public void ShouldReturnViewShouldThrowExceptionIfActionResultIsViewResultWithDi
33
76
} ,
34
77
"When calling IndexView action in MvcController expected view result to be 'Incorrect', but instead received 'Index'." ) ;
35
78
}
36
-
79
+
80
+ [ Fact ]
81
+ public void ShouldReturnViewShouldThrowExceptionWithNullViewName ( )
82
+ {
83
+ Test . AssertException < ViewResultAssertionException > (
84
+ ( ) =>
85
+ {
86
+ MyController < MvcController >
87
+ . Instance ( )
88
+ . Calling ( c => c . IndexView ( ) )
89
+ . ShouldReturn ( )
90
+ . View ( view => view
91
+ . WithName ( null ) ) ;
92
+ } ,
93
+ "When calling IndexView action in MvcController expected view result to be the default one, but instead received 'Index'." ) ;
94
+ }
95
+
96
+ [ Fact ]
97
+ public void ShouldReturnViewShouldThrowExceptionWithIncorrectViewResultAndName ( )
98
+ {
99
+ Test . AssertException < InvocationResultAssertionException > (
100
+ ( ) =>
101
+ {
102
+ MyController < MvcController >
103
+ . Instance ( )
104
+ . Calling ( c => c . BadRequestAction ( ) )
105
+ . ShouldReturn ( )
106
+ . View ( view => view
107
+ . WithName ( "TestView" ) ) ;
108
+ } ,
109
+ "When calling BadRequestAction action in MvcController expected result to be ViewResult, but instead received BadRequestResult." ) ;
110
+ }
111
+
37
112
[ Fact ]
38
113
public void ShouldReturnPartialViewWithNameShouldNotThrowExceptionWithCorrectName ( )
39
114
{
@@ -44,7 +119,23 @@ public void ShouldReturnPartialViewWithNameShouldNotThrowExceptionWithCorrectNam
44
119
. PartialView ( view => view
45
120
. WithName ( "_IndexPartial" ) ) ;
46
121
}
47
-
122
+
123
+ [ Fact ]
124
+ public void ShouldReturnPartialViewWithNameShouldNotThrowExceptionAndPassAssertions ( )
125
+ {
126
+ MyController < MvcController >
127
+ . Instance ( )
128
+ . Calling ( c => c . IndexPartialView ( ) )
129
+ . ShouldReturn ( )
130
+ . PartialView ( view => view
131
+ . Passing ( v =>
132
+ {
133
+ Assert . False ( string . IsNullOrEmpty ( v . ViewName ) ) ;
134
+ Assert . Equal ( "_IndexPartial" , v . ViewName ) ;
135
+ Assert . True ( typeof ( PartialViewResult ) == v . GetType ( ) ) ;
136
+ } ) ) ;
137
+ }
138
+
48
139
[ Fact ]
49
140
public void ShouldReturnPartialViewShouldThrowExceptionIfActionResultIsViewResultWithDifferentName ( )
50
141
{
@@ -60,5 +151,127 @@ public void ShouldReturnPartialViewShouldThrowExceptionIfActionResultIsViewResul
60
151
} ,
61
152
"When calling IndexPartialView action in MvcController expected partial view result to be 'Incorrect', but instead received '_IndexPartial'." ) ;
62
153
}
154
+
155
+ [ Fact ]
156
+ public void ShouldReturnPartialViewShouldThrowExceptionWithNullViewName ( )
157
+ {
158
+ Test . AssertException < ViewResultAssertionException > (
159
+ ( ) =>
160
+ {
161
+ MyController < MvcController >
162
+ . Instance ( )
163
+ . Calling ( c => c . IndexPartialView ( ) )
164
+ . ShouldReturn ( )
165
+ . PartialView ( view => view
166
+ . WithName ( null ) ) ;
167
+ } ,
168
+ "When calling IndexPartialView action in MvcController expected partial view result to be the default one, but instead received '_IndexPartial'." ) ;
169
+ }
170
+
171
+ [ Fact ]
172
+ public void ShouldReturnParrtialViewShouldThrowExceptionWithIncorrectViewResult ( )
173
+ {
174
+ Test . AssertException < InvocationResultAssertionException > (
175
+ ( ) =>
176
+ {
177
+ MyController < MvcController >
178
+ . Instance ( )
179
+ . Calling ( c => c . BadRequestAction ( ) )
180
+ . ShouldReturn ( )
181
+ . PartialView ( view => view
182
+ . WithName ( "_IndexPartial" ) ) ;
183
+ } ,
184
+ "When calling BadRequestAction action in MvcController expected result to be PartialViewResult, but instead received BadRequestResult." ) ;
185
+ }
186
+
187
+ [ Fact ]
188
+ public void ShouldReturnViewWithDefaultNameShouldNotThrowException ( )
189
+ {
190
+ MyController < MvcController >
191
+ . Instance ( )
192
+ . Calling ( c => c . DefaultView ( ) )
193
+ . ShouldReturn ( )
194
+ . View ( view => view
195
+ . WithDefaultName ( ) ) ;
196
+ }
197
+
198
+ [ Fact ]
199
+ public void ShouldReturnViewWithDefaultNameShouldNotThrowExceptionAndPassAssertions ( )
200
+ {
201
+ MyController < MvcController >
202
+ . Instance ( )
203
+ . Calling ( c => c . DefaultView ( ) )
204
+ . ShouldReturn ( )
205
+ . View ( view => view
206
+ . Passing ( v =>
207
+ {
208
+ Assert . True ( string . IsNullOrEmpty ( v . ViewName ) ) ;
209
+ Assert . True ( v . GetType ( ) == typeof ( ViewResult ) ) ;
210
+ } ) ) ;
211
+ }
212
+
213
+ [ Fact ]
214
+ public void ShouldReturnViewWithDefaultNameAndViewModelShouldNotThrowException ( )
215
+ {
216
+ MyController < MvcController >
217
+ . Instance ( )
218
+ . Calling ( c => c . DefaultViewWithModel ( ) )
219
+ . ShouldReturn ( )
220
+ . View ( view => view
221
+ . WithDefaultName ( )
222
+ . AndAlso ( )
223
+ . WithModelOfType < ICollection < ResponseModel > > ( ) ) ;
224
+ }
225
+
226
+ [ Fact ]
227
+ public void ShouldReturnViewWithDefaultNameAndCustomViewResultTypeShouldNotThrowExceptionWithCorrectType ( )
228
+ {
229
+ MyController < MvcController >
230
+ . Instance ( )
231
+ . Calling ( c => c . CustomViewResult ( ) )
232
+ . ShouldReturn ( )
233
+ . View ( view => view
234
+ . WithDefaultName ( )
235
+ . Passing ( v => v . GetType ( ) == typeof ( ViewResult ) ) ) ;
236
+ }
237
+
238
+ [ Fact ]
239
+ public void ShouldReturnPartialViewWithDefaultNameShouldNotThrowException ( )
240
+ {
241
+ MyController < MvcController >
242
+ . Instance ( )
243
+ . Calling ( c => c . DefaultPartialView ( ) )
244
+ . ShouldReturn ( )
245
+ . PartialView ( view => view
246
+ . WithDefaultName ( ) ) ;
247
+ }
248
+
249
+ [ Fact ]
250
+ public void ShouldReturnPartialViewWithDefaultNameShouldNotThrowExceptionAndPassAssertions ( )
251
+ {
252
+ MyController < MvcController >
253
+ . Instance ( )
254
+ . Calling ( c => c . DefaultPartialView ( ) )
255
+ . ShouldReturn ( )
256
+ . PartialView ( view => view
257
+ . Passing ( v =>
258
+ {
259
+ Assert . True ( string . IsNullOrEmpty ( v . ViewName ) ) ;
260
+ Assert . True ( v . GetType ( ) == typeof ( PartialViewResult ) ) ;
261
+ } ) ) ;
262
+ }
263
+
264
+ [ Fact ]
265
+ public void ShouldReturnPartialViewWithDefaultNameAndViewModelShouldNotThrowException ( )
266
+ {
267
+ MyController < MvcController >
268
+ . Instance ( )
269
+ . Calling ( c => c . DefaultPartialViewWithModel ( ) )
270
+ . ShouldReturn ( )
271
+ . PartialView ( view => view
272
+ . WithDefaultName ( )
273
+ . AndAlso ( )
274
+ . WithModelOfType < ICollection < ResponseModel > > ( ) ) ;
275
+ }
63
276
}
64
277
}
0 commit comments