1
1
// Copyright (c) .NET Foundation. All rights reserved.
2
2
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3
3
4
+ using System ;
5
+ using System . Collections . Generic ;
4
6
using System . Threading . Tasks ;
5
7
using Microsoft . AspNet . Mvc . Abstractions ;
6
8
using Microsoft . AspNet . Mvc . Formatters ;
@@ -132,5 +134,139 @@ public async Task BindParameterFromService_WithData_GetsBound()
132
134
Assert . True ( modelState . IsValid ) ;
133
135
Assert . Empty ( modelState . Keys ) ;
134
136
}
137
+
138
+ [ Fact ]
139
+ public async Task BindParameterFromService_NoPrefix_GetsBound ( )
140
+ {
141
+ // Arrange
142
+ var argumentBinder = ModelBindingTestHelper . GetArgumentBinder ( ) ;
143
+ var parameter = new ParameterDescriptor
144
+ {
145
+ Name = "ControllerProperty" ,
146
+ BindingInfo = new BindingInfo
147
+ {
148
+ BindingSource = BindingSource . Services ,
149
+ } ,
150
+
151
+ // Use a service type already in defaults.
152
+ ParameterType = typeof ( JsonOutputFormatter ) ,
153
+ } ;
154
+
155
+ var operationContext = ModelBindingTestHelper . GetOperationBindingContext ( ) ;
156
+ var modelState = new ModelStateDictionary ( ) ;
157
+
158
+ // Act
159
+ var modelBindingResult = await argumentBinder . BindModelAsync ( parameter , modelState , operationContext ) ;
160
+
161
+ // Assert
162
+ // ModelBindingResult
163
+ Assert . True ( modelBindingResult . IsModelSet ) ;
164
+
165
+ // Model
166
+ var outputFormatter = Assert . IsType < JsonOutputFormatter > ( modelBindingResult . Model ) ;
167
+ Assert . NotNull ( outputFormatter ) ;
168
+
169
+ // ModelState
170
+ Assert . True ( modelState . IsValid ) ;
171
+ Assert . Empty ( modelState ) ;
172
+ }
173
+
174
+ [ Fact ]
175
+ public async Task BindEnumerableParameterFromService_NoPrefix_GetsBound ( )
176
+ {
177
+ // Arrange
178
+ var argumentBinder = ModelBindingTestHelper . GetArgumentBinder ( ) ;
179
+ var parameter = new ParameterDescriptor
180
+ {
181
+ Name = "ControllerProperty" ,
182
+ BindingInfo = new BindingInfo
183
+ {
184
+ BindingSource = BindingSource . Services ,
185
+ } ,
186
+
187
+ // Use a service type already in defaults.
188
+ ParameterType = typeof ( IEnumerable < JsonOutputFormatter > ) ,
189
+ } ;
190
+
191
+ var operationContext = ModelBindingTestHelper . GetOperationBindingContext ( ) ;
192
+ var modelState = new ModelStateDictionary ( ) ;
193
+
194
+ // Act
195
+ var modelBindingResult = await argumentBinder . BindModelAsync ( parameter , modelState , operationContext ) ;
196
+
197
+ // Assert
198
+ // ModelBindingResult
199
+ Assert . True ( modelBindingResult . IsModelSet ) ;
200
+
201
+ // Model
202
+ var formatterArray = Assert . IsType < JsonOutputFormatter [ ] > ( modelBindingResult . Model ) ;
203
+ Assert . Equal ( 1 , formatterArray . Length ) ;
204
+
205
+ // ModelState
206
+ Assert . True ( modelState . IsValid ) ;
207
+ Assert . Empty ( modelState ) ;
208
+ }
209
+
210
+ [ Fact ]
211
+ public async Task BindEnumerableParameterFromService_NoService_GetsBound ( )
212
+ {
213
+ // Arrange
214
+ var argumentBinder = ModelBindingTestHelper . GetArgumentBinder ( ) ;
215
+ var parameter = new ParameterDescriptor
216
+ {
217
+ Name = "ControllerProperty" ,
218
+ BindingInfo = new BindingInfo
219
+ {
220
+ BindingSource = BindingSource . Services ,
221
+ } ,
222
+
223
+ // Use a service type not available in DI.
224
+ ParameterType = typeof ( IEnumerable < IActionResult > ) ,
225
+ } ;
226
+
227
+ var operationContext = ModelBindingTestHelper . GetOperationBindingContext ( ) ;
228
+ var modelState = new ModelStateDictionary ( ) ;
229
+
230
+ // Act
231
+ var modelBindingResult = await argumentBinder . BindModelAsync ( parameter , modelState , operationContext ) ;
232
+
233
+ // Assert
234
+ // ModelBindingResult
235
+ Assert . True ( modelBindingResult . IsModelSet ) ;
236
+
237
+ // Model
238
+ var actionResultArray = Assert . IsType < IActionResult [ ] > ( modelBindingResult . Model ) ;
239
+ Assert . Equal ( 0 , actionResultArray . Length ) ;
240
+
241
+ // ModelState
242
+ Assert . True ( modelState . IsValid ) ;
243
+ Assert . Empty ( modelState ) ;
244
+ }
245
+
246
+ [ Fact ]
247
+ public async Task BindParameterFromService_NoService_Throws ( )
248
+ {
249
+ // Arrange
250
+ var argumentBinder = ModelBindingTestHelper . GetArgumentBinder ( ) ;
251
+ var parameter = new ParameterDescriptor
252
+ {
253
+ Name = "ControllerProperty" ,
254
+ BindingInfo = new BindingInfo
255
+ {
256
+ BindingSource = BindingSource . Services ,
257
+ } ,
258
+
259
+ // Use a service type not available in DI.
260
+ ParameterType = typeof ( IActionResult ) ,
261
+ } ;
262
+
263
+ var operationContext = ModelBindingTestHelper . GetOperationBindingContext ( ) ;
264
+ var modelState = new ModelStateDictionary ( ) ;
265
+
266
+ // Act & Assert
267
+ var exception = await Assert . ThrowsAsync < InvalidOperationException > (
268
+ ( ) => argumentBinder . BindModelAsync ( parameter , modelState , operationContext ) ) ;
269
+ Assert . Contains ( typeof ( IActionResult ) . FullName , exception . Message ) ;
270
+ }
135
271
}
136
272
}
0 commit comments