From 4df7d52210555f3fe2264241a5d53ed5e5e3cf06 Mon Sep 17 00:00:00 2001 From: Harsh Gupta Date: Tue, 21 Apr 2015 15:44:18 -0700 Subject: [PATCH] PR Comments --- .../ControllerActionArgumentBinderTests.cs | 47 +++++++++++++++++-- 1 file changed, 44 insertions(+), 3 deletions(-) diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/ParameterBinding/ControllerActionArgumentBinderTests.cs b/test/Microsoft.AspNet.Mvc.Core.Test/ParameterBinding/ControllerActionArgumentBinderTests.cs index d1df43d745..9abc66e547 100644 --- a/test/Microsoft.AspNet.Mvc.Core.Test/ParameterBinding/ControllerActionArgumentBinderTests.cs +++ b/test/Microsoft.AspNet.Mvc.Core.Test/ParameterBinding/ControllerActionArgumentBinderTests.cs @@ -318,7 +318,7 @@ namespace Microsoft.AspNet.Mvc.Core.Test [Fact] - public async Task BindActionArgumentsAsync_SetsControllerProperties() + public async Task BindActionArgumentsAsync_SetsControllerProperties_ForReferenceTypes() { // Arrange var actionDescriptor = GetActionDescriptor(); @@ -351,8 +351,8 @@ namespace Microsoft.AspNet.Mvc.Core.Test actionDescriptor.BoundProperties.Add( new ParameterDescriptor { - Name = "ValueBinderMarkedProperty", - BindingInfo = new BindingInfo(), + Name = "NotNullableProperty", + BindingInfo = new BindingInfo() { BindingSource = BindingSource.Custom }, ParameterType = typeof(int) }); @@ -381,6 +381,44 @@ namespace Microsoft.AspNet.Mvc.Core.Test Assert.Equal(-1, controller.NotNullableProperty); } + [Fact] + public async Task BindActionArgumentsAsync_SetsNullValues_ForNullableProperties() + { + // Arrange + var actionDescriptor = GetActionDescriptor(); + actionDescriptor.BoundProperties.Add( + new ParameterDescriptor + { + Name = "NullableProperty", + BindingInfo = new BindingInfo() { BindingSource = BindingSource.Custom }, + ParameterType = typeof(int?) + }); + + var actionContext = GetActionContext(actionDescriptor); + + var binder = new Mock(); + binder + .Setup(b => b.BindModelAsync(It.IsAny())) + .Returns(Task.FromResult( + result: new ModelBindingResult(model: null, key: string.Empty, isModelSet: true))); + var actionBindingContext = new ActionBindingContext() + { + ModelBinder = binder.Object, + }; + + var argumentBinder = GetArgumentBinder(); + var controller = new TestController(); + + // Some non default value. + controller.NullableProperty = -1; + + // Act + var result = await argumentBinder.BindActionArgumentsAsync(actionContext, actionBindingContext, controller); + + // Assert + Assert.Null(controller.NullableProperty); + } + private static ActionContext GetActionContext(ActionDescriptor descriptor = null) { return new ActionContext( @@ -441,6 +479,9 @@ namespace Microsoft.AspNet.Mvc.Core.Test [CustomBindingSource] public int NotNullableProperty { get; set; } + [CustomBindingSource] + public int? NullableProperty { get; set; } + public Person ActionWithBodyParam([FromBody] Person bodyParam) { return bodyParam;