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`
This commit is contained in:
Doug Bunting 2016-02-24 08:48:01 -08:00
parent 385c21fbe2
commit 49ffeb16d2
2 changed files with 24 additions and 5 deletions

View File

@ -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<IList<int>>().BindingDetails(bd => bd.IsReadOnly = isReadOnly);
metadataProvider
.ForProperty<ModelWithIListProperty>(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<int>)),
ModelMetadata = metadata,
ModelName = "someName",
ModelState = new ModelStateDictionary(),
ValueProvider = valueProvider,
@ -431,6 +435,11 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding
public List<string> ListProperty { get; set; }
}
private class ModelWithIListProperty
{
public IList<int> ListProperty { get; set; }
}
private class ModelWithSimpleProperties
{
public int Id { get; set; }

View File

@ -472,7 +472,12 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Test
IDictionary<string, KeyValuePair<int, string>> values)
{
var metadataProvider = new TestModelMetadataProvider();
metadataProvider.ForType<IDictionary<int, string>>().BindingDetails(bd => bd.IsReadOnly = isReadOnly);
metadataProvider
.ForProperty<ModelWithIDictionaryProperty>(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<int, string>)),
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<int, string> DictionaryProperty { get; set; }
}
private class ModelWithDictionaryProperties
{
// A Dictionary<string, string> instance cannot be assigned to this property.