diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/ModelBinding/ServiceModelBinderTest.cs b/test/Microsoft.AspNet.Mvc.Core.Test/ModelBinding/ServicesModelBinderTest.cs similarity index 98% rename from test/Microsoft.AspNet.Mvc.Core.Test/ModelBinding/ServiceModelBinderTest.cs rename to test/Microsoft.AspNet.Mvc.Core.Test/ModelBinding/ServicesModelBinderTest.cs index 629239fafd..732d6ba1c0 100644 --- a/test/Microsoft.AspNet.Mvc.Core.Test/ModelBinding/ServiceModelBinderTest.cs +++ b/test/Microsoft.AspNet.Mvc.Core.Test/ModelBinding/ServicesModelBinderTest.cs @@ -10,7 +10,7 @@ using Xunit; namespace Microsoft.AspNet.Mvc.ModelBinding { - public class ServiceModelBinderTest + public class ServicesModelBinderTest { [Fact] public async Task ServiceModelBinder_BindsService() diff --git a/test/Microsoft.AspNet.Mvc.IntegrationTests/ServicesModelBinderIntegrationTest.cs b/test/Microsoft.AspNet.Mvc.IntegrationTests/ServicesModelBinderIntegrationTest.cs index 3aa5b02c2f..3e8ea213fc 100644 --- a/test/Microsoft.AspNet.Mvc.IntegrationTests/ServicesModelBinderIntegrationTest.cs +++ b/test/Microsoft.AspNet.Mvc.IntegrationTests/ServicesModelBinderIntegrationTest.cs @@ -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(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), + }; + + 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(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), + }; + + 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(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( + () => argumentBinder.BindModelAsync(parameter, modelState, operationContext)); + Assert.Contains(typeof(IActionResult).FullName, exception.Message); + } } } \ No newline at end of file