From 49ffeb16d22fad67a14630d7f4aba29f7d7196d0 Mon Sep 17 00:00:00 2001 From: Doug Bunting Date: Wed, 24 Feb 2016 08:48:01 -0800 Subject: [PATCH] Test fix: Change `CollectionModelBinderTest` to update `ModelMetadata.IsReadOnly` - unrelated to #3482 except that I discovered the issue while investigating that issue - tests previously set `BindingDetails.IsReadOnly` for a `Type` and that was ignored - same for `DictionaryModelBinderTest` --- .../ModelBinding/CollectionModelBinderTest.cs | 15 ++++++++++++--- .../ModelBinding/DictionaryModelBinderTest.cs | 14 ++++++++++++-- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/test/Microsoft.AspNetCore.Mvc.Core.Test/ModelBinding/CollectionModelBinderTest.cs b/test/Microsoft.AspNetCore.Mvc.Core.Test/ModelBinding/CollectionModelBinderTest.cs index fdc17cf9f8..c6d3c9796d 100644 --- a/test/Microsoft.AspNetCore.Mvc.Core.Test/ModelBinding/CollectionModelBinderTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.Core.Test/ModelBinding/CollectionModelBinderTest.cs @@ -10,7 +10,6 @@ using Microsoft.AspNetCore.Http.Internal; using Microsoft.AspNetCore.Mvc.Internal; using Microsoft.AspNetCore.Mvc.ModelBinding.Test; using Microsoft.AspNetCore.Mvc.ModelBinding.Validation; -using Moq; using Xunit; namespace Microsoft.AspNetCore.Mvc.ModelBinding @@ -367,11 +366,16 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding bool isReadOnly = false) { var metadataProvider = new TestModelMetadataProvider(); - metadataProvider.ForType>().BindingDetails(bd => bd.IsReadOnly = isReadOnly); + metadataProvider + .ForProperty(nameof(ModelWithIListProperty.ListProperty)) + .BindingDetails(bd => bd.IsReadOnly = isReadOnly); + var metadata = metadataProvider.GetMetadataForProperty( + typeof(ModelWithIListProperty), + nameof(ModelWithIListProperty.ListProperty)); var bindingContext = new DefaultModelBindingContext { - ModelMetadata = metadataProvider.GetMetadataForType(typeof(IList)), + ModelMetadata = metadata, ModelName = "someName", ModelState = new ModelStateDictionary(), ValueProvider = valueProvider, @@ -431,6 +435,11 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding public List ListProperty { get; set; } } + private class ModelWithIListProperty + { + public IList ListProperty { get; set; } + } + private class ModelWithSimpleProperties { public int Id { get; set; } diff --git a/test/Microsoft.AspNetCore.Mvc.Core.Test/ModelBinding/DictionaryModelBinderTest.cs b/test/Microsoft.AspNetCore.Mvc.Core.Test/ModelBinding/DictionaryModelBinderTest.cs index 50c086eabd..d9ad2333ec 100644 --- a/test/Microsoft.AspNetCore.Mvc.Core.Test/ModelBinding/DictionaryModelBinderTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.Core.Test/ModelBinding/DictionaryModelBinderTest.cs @@ -472,7 +472,12 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Test IDictionary> values) { var metadataProvider = new TestModelMetadataProvider(); - metadataProvider.ForType>().BindingDetails(bd => bd.IsReadOnly = isReadOnly); + metadataProvider + .ForProperty(nameof(ModelWithIDictionaryProperty.DictionaryProperty)) + .BindingDetails(bd => bd.IsReadOnly = isReadOnly); + var metadata = metadataProvider.GetMetadataForProperty( + typeof(ModelWithIDictionaryProperty), + nameof(ModelWithIDictionaryProperty.DictionaryProperty)); var binder = new StubModelBinder(mbc => { @@ -491,7 +496,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Test var bindingContext = new DefaultModelBindingContext { - ModelMetadata = metadataProvider.GetMetadataForType(typeof(IDictionary)), + ModelMetadata = metadata, ModelName = "someName", ModelState = new ModelStateDictionary(), OperationBindingContext = new OperationBindingContext @@ -507,6 +512,11 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Test return bindingContext; } + private class ModelWithIDictionaryProperty + { + public IDictionary DictionaryProperty { get; set; } + } + private class ModelWithDictionaryProperties { // A Dictionary instance cannot be assigned to this property.