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
This commit is contained in:
Doug Bunting 2015-10-14 11:46:11 -07:00
parent 9aed5efd51
commit 2e2043f427
2 changed files with 137 additions and 1 deletions

View File

@ -10,7 +10,7 @@ using Xunit;
namespace Microsoft.AspNet.Mvc.ModelBinding
{
public class ServiceModelBinderTest
public class ServicesModelBinderTest
{
[Fact]
public async Task ServiceModelBinder_BindsService()

View File

@ -1,6 +1,8 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNet.Mvc.Abstractions;
using Microsoft.AspNet.Mvc.Formatters;
@ -132,5 +134,139 @@ namespace Microsoft.AspNet.Mvc.IntegrationTests
Assert.True(modelState.IsValid);
Assert.Empty(modelState.Keys);
}
[Fact]
public async Task BindParameterFromService_NoPrefix_GetsBound()
{
// Arrange
var argumentBinder = ModelBindingTestHelper.GetArgumentBinder();
var parameter = new ParameterDescriptor
{
Name = "ControllerProperty",
BindingInfo = new BindingInfo
{
BindingSource = BindingSource.Services,
},
// Use a service type already in defaults.
ParameterType = typeof(JsonOutputFormatter),
};
var operationContext = ModelBindingTestHelper.GetOperationBindingContext();
var modelState = new ModelStateDictionary();
// Act
var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext);
// Assert
// ModelBindingResult
Assert.True(modelBindingResult.IsModelSet);
// Model
var outputFormatter = Assert.IsType<JsonOutputFormatter>(modelBindingResult.Model);
Assert.NotNull(outputFormatter);
// ModelState
Assert.True(modelState.IsValid);
Assert.Empty(modelState);
}
[Fact]
public async Task BindEnumerableParameterFromService_NoPrefix_GetsBound()
{
// Arrange
var argumentBinder = ModelBindingTestHelper.GetArgumentBinder();
var parameter = new ParameterDescriptor
{
Name = "ControllerProperty",
BindingInfo = new BindingInfo
{
BindingSource = BindingSource.Services,
},
// Use a service type already in defaults.
ParameterType = typeof(IEnumerable<JsonOutputFormatter>),
};
var operationContext = ModelBindingTestHelper.GetOperationBindingContext();
var modelState = new ModelStateDictionary();
// Act
var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext);
// Assert
// ModelBindingResult
Assert.True(modelBindingResult.IsModelSet);
// Model
var formatterArray = Assert.IsType<JsonOutputFormatter[]>(modelBindingResult.Model);
Assert.Equal(1, formatterArray.Length);
// ModelState
Assert.True(modelState.IsValid);
Assert.Empty(modelState);
}
[Fact]
public async Task BindEnumerableParameterFromService_NoService_GetsBound()
{
// Arrange
var argumentBinder = ModelBindingTestHelper.GetArgumentBinder();
var parameter = new ParameterDescriptor
{
Name = "ControllerProperty",
BindingInfo = new BindingInfo
{
BindingSource = BindingSource.Services,
},
// Use a service type not available in DI.
ParameterType = typeof(IEnumerable<IActionResult>),
};
var operationContext = ModelBindingTestHelper.GetOperationBindingContext();
var modelState = new ModelStateDictionary();
// Act
var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext);
// Assert
// ModelBindingResult
Assert.True(modelBindingResult.IsModelSet);
// Model
var actionResultArray = Assert.IsType<IActionResult[]>(modelBindingResult.Model);
Assert.Equal(0, actionResultArray.Length);
// ModelState
Assert.True(modelState.IsValid);
Assert.Empty(modelState);
}
[Fact]
public async Task BindParameterFromService_NoService_Throws()
{
// Arrange
var argumentBinder = ModelBindingTestHelper.GetArgumentBinder();
var parameter = new ParameterDescriptor
{
Name = "ControllerProperty",
BindingInfo = new BindingInfo
{
BindingSource = BindingSource.Services,
},
// Use a service type not available in DI.
ParameterType = typeof(IActionResult),
};
var operationContext = ModelBindingTestHelper.GetOperationBindingContext();
var modelState = new ModelStateDictionary();
// Act & Assert
var exception = await Assert.ThrowsAsync<InvalidOperationException>(
() => argumentBinder.BindModelAsync(parameter, modelState, operationContext));
Assert.Contains(typeof(IActionResult).FullName, exception.Message);
}
}
}