Skip to content
This repository was archived by the owner on Dec 14, 2018. It is now read-only.

Commit 2e2043f

Browse files
committed
Test [FromServices] with service that is not available
- test `[FromServices]` for a defined type without a `BinderModelName` - test `[FromServices]` for a service not available in DI - test `[FromServices]` for `IEnumerable<TService>` properties nit: correct name of `ServicesModelBinderTest` to match the model binder class
1 parent 9aed5ef commit 2e2043f

File tree

2 files changed

+137
-1
lines changed

2 files changed

+137
-1
lines changed

test/Microsoft.AspNet.Mvc.Core.Test/ModelBinding/ServiceModelBinderTest.cs renamed to test/Microsoft.AspNet.Mvc.Core.Test/ModelBinding/ServicesModelBinderTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
namespace Microsoft.AspNet.Mvc.ModelBinding
1212
{
13-
public class ServiceModelBinderTest
13+
public class ServicesModelBinderTest
1414
{
1515
[Fact]
1616
public async Task ServiceModelBinder_BindsService()

test/Microsoft.AspNet.Mvc.IntegrationTests/ServicesModelBinderIntegrationTest.cs

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
using System;
5+
using System.Collections.Generic;
46
using System.Threading.Tasks;
57
using Microsoft.AspNet.Mvc.Abstractions;
68
using Microsoft.AspNet.Mvc.Formatters;
@@ -132,5 +134,139 @@ public async Task BindParameterFromService_WithData_GetsBound()
132134
Assert.True(modelState.IsValid);
133135
Assert.Empty(modelState.Keys);
134136
}
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+
}
135271
}
136272
}

0 commit comments

Comments
 (0)