public void ModelNameProperty() { // Arrange ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext(); // Act & assert MemberHelper.TestStringProperty(bindingContext, "ModelName", String.Empty); }
public void TryBindStrongModel_BinderExists_BinderReturnsIncorrectlyTypedObject_ReturnsTrue() { // Arrange ControllerContext controllerContext = new ControllerContext(); ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext() { ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(int)), ModelName = "someName", ModelState = new ModelStateDictionary(), ModelBinderProviders = new ModelBinderProviderCollection(), ValueProvider = new SimpleValueProvider() }; Mock<IExtensibleModelBinder> mockIntBinder = new Mock<IExtensibleModelBinder>(); mockIntBinder .Setup(o => o.BindModel(controllerContext, It.IsAny<ExtensibleModelBindingContext>())) .Returns( delegate(ControllerContext cc, ExtensibleModelBindingContext mbc) { Assert.AreEqual("someName.key", mbc.ModelName); return true; }); bindingContext.ModelBinderProviders.RegisterBinderForType(typeof(int), mockIntBinder.Object, true /* suppressPrefixCheck */); // Act int model; bool retVal = KeyValuePairModelBinderUtil.TryBindStrongModel<int>(controllerContext, bindingContext, "key", new EmptyModelMetadataProvider(), out model); // Assert Assert.IsTrue(retVal); Assert.AreEqual(default(int), model); Assert.AreEqual(1, bindingContext.ValidationNode.ChildNodes.Count, "Child validation node should have been added."); Assert.AreEqual(0, bindingContext.ModelState.Count, "ModelState should remain unmodified."); }
public void ModelBinderProvidersProperty() { // Arrange ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext(); // Act & assert MemberHelper.TestPropertyWithDefaultInstance(bindingContext, "ModelBinderProviders", new ModelBinderProviderCollection(), ModelBinderProviders.Providers); }
public void BindModel() { // Arrange ControllerContext controllerContext = new ControllerContext(); ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext() { ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(int[])), ModelName = "someName", ModelBinderProviders = new ModelBinderProviderCollection(), ValueProvider = new SimpleValueProvider() { { "someName[0]", "42" }, { "someName[1]", "84" } } }; Mock<IExtensibleModelBinder> mockIntBinder = new Mock<IExtensibleModelBinder>(); mockIntBinder .Expect(o => o.BindModel(controllerContext, It.IsAny<ExtensibleModelBindingContext>())) .Returns( delegate(ControllerContext cc, ExtensibleModelBindingContext mbc) { mbc.Model = mbc.ValueProvider.GetValue(mbc.ModelName).ConvertTo(mbc.ModelType); return true; }); bindingContext.ModelBinderProviders.RegisterBinderForType(typeof(int), mockIntBinder.Object, false /* suppressPrefixCheck */); // Act bool retVal = new ArrayModelBinder<int>().BindModel(controllerContext, bindingContext); // Assert Assert.IsTrue(retVal); int[] array = bindingContext.Model as int[]; CollectionAssert.AreEqual(new int[] { 42, 84 }, array); }
public void BindComplexCollectionFromIndexes_FiniteIndexes() { // Arrange ControllerContext controllerContext = new ControllerContext(); CultureInfo culture = CultureInfo.GetCultureInfo("fr-FR"); ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext { ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(int)), ModelName = "someName", ModelBinderProviders = new ModelBinderProviderCollection(), ValueProvider = new SimpleValueProvider { { "someName[foo]", "42" }, { "someName[baz]", "200" } } }; Mock<IExtensibleModelBinder> mockIntBinder = new Mock<IExtensibleModelBinder>(); mockIntBinder .Setup(o => o.BindModel(controllerContext, It.IsAny<ExtensibleModelBindingContext>())) .Returns( delegate(ControllerContext cc, ExtensibleModelBindingContext mbc) { mbc.Model = mbc.ValueProvider.GetValue(mbc.ModelName).ConvertTo(mbc.ModelType); return true; }); bindingContext.ModelBinderProviders.RegisterBinderForType(typeof(int), mockIntBinder.Object, false /* suppressPrefixCheck */); // Act List<int> boundCollection = CollectionModelBinder<int>.BindComplexCollectionFromIndexes(controllerContext, bindingContext, new[] { "foo", "bar", "baz" }); // Assert Assert.Equal(new[] { 42, 0, 200 }, boundCollection.ToArray()); Assert.Equal(new[] { "someName[foo]", "someName[baz]" }, bindingContext.ValidationNode.ChildNodes.Select(o => o.ModelStateKey).ToArray()); }
public void BindModel_MissingValue_ReturnsTrue() { // Arrange ControllerContext controllerContext = new ControllerContext(); ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext() { ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(KeyValuePair<int, string>)), ModelName = "someName", ModelBinderProviders = new ModelBinderProviderCollection(), ValueProvider = new SimpleValueProvider() }; Mock<IExtensibleModelBinder> mockIntBinder = new Mock<IExtensibleModelBinder>(); mockIntBinder .Setup(o => o.BindModel(controllerContext, It.IsAny<ExtensibleModelBindingContext>())) .Returns( delegate(ControllerContext cc, ExtensibleModelBindingContext mbc) { mbc.Model = 42; return true; }); bindingContext.ModelBinderProviders.RegisterBinderForType(typeof(int), mockIntBinder.Object, true /* suppressPrefixCheck */); KeyValuePairModelBinder<int, string> binder = new KeyValuePairModelBinder<int, string>(); // Act bool retVal = binder.BindModel(controllerContext, bindingContext); // Assert Assert.IsTrue(retVal, "Want to return 'True' since the key was bound correctly and we want to push validation up."); Assert.IsNull(bindingContext.Model); CollectionAssert.AreEquivalent(new string[] { "someName.key" }, bindingContext.ValidationNode.ChildNodes.Select(n => n.ModelStateKey).ToArray()); }
public void ModelNameProperty() { // Arrange ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext(); // Act & assert MemberHelper.TestStringProperty(bindingContext, "ModelName", String.Empty, false /* testDefaultValue */, true /* allowNullAndEmpty */); }
public void BindModel() { // Arrange ControllerContext controllerContext = new ControllerContext(); ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext() { ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(IDictionary<int, string>)), ModelName = "someName", ModelBinderProviders = new ModelBinderProviderCollection(), ValueProvider = new SimpleValueProvider() { { "someName[0]", new KeyValuePair<int, string>(42, "forty-two") }, { "someName[1]", new KeyValuePair<int, string>(84, "eighty-four") } } }; Mock<IExtensibleModelBinder> mockKvpBinder = new Mock<IExtensibleModelBinder>(); mockKvpBinder .Expect(o => o.BindModel(controllerContext, It.IsAny<ExtensibleModelBindingContext>())) .Returns( delegate(ControllerContext cc, ExtensibleModelBindingContext mbc) { mbc.Model = mbc.ValueProvider.GetValue(mbc.ModelName).ConvertTo(mbc.ModelType); return true; }); bindingContext.ModelBinderProviders.RegisterBinderForType(typeof(KeyValuePair<int, string>), mockKvpBinder.Object, false /* suppressPrefixCheck */); // Act bool retVal = new DictionaryModelBinder<int, string>().BindModel(controllerContext, bindingContext); // Assert Assert.IsTrue(retVal); IDictionary<int, string> dictionary = bindingContext.Model as IDictionary<int, string>; Assert.IsNotNull(dictionary); Assert.AreEqual(2, dictionary.Count); Assert.AreEqual("forty-two", dictionary[42]); Assert.AreEqual("eighty-four", dictionary[84]); }
public void BindComplexCollectionFromIndexes_InfiniteIndexes() { // Arrange ControllerContext controllerContext = new ControllerContext(); CultureInfo culture = CultureInfo.GetCultureInfo("fr-FR"); ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext() { ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(int)), ModelName = "someName", ModelBinderProviders = new ModelBinderProviderCollection(), ValueProvider = new SimpleValueProvider() { { "someName[0]", "42" }, { "someName[1]", "100" }, { "someName[3]", "400" } } }; Mock<IExtensibleModelBinder> mockIntBinder = new Mock<IExtensibleModelBinder>(); mockIntBinder .Expect(o => o.BindModel(controllerContext, It.IsAny<ExtensibleModelBindingContext>())) .Returns( delegate(ControllerContext cc, ExtensibleModelBindingContext mbc) { mbc.Model = mbc.ValueProvider.GetValue(mbc.ModelName).ConvertTo(mbc.ModelType); return true; }); bindingContext.ModelBinderProviders.RegisterBinderForType(typeof(int), mockIntBinder.Object, false /* suppressPrefixCheck */); // Act List<int> boundCollection = CollectionModelBinder<int>.BindComplexCollectionFromIndexes(controllerContext, bindingContext, null /* indexNames */); // Assert CollectionAssert.AreEqual(new int[] { 42, 100 }, boundCollection, "Binding should have halted at missing element."); CollectionAssert.AreEquivalent(new string[] { "someName[0]", "someName[1]" }, bindingContext.ValidationNode.ChildNodes.Select(o => o.ModelStateKey).ToArray()); }
public void BindModel() { // Arrange ControllerContext controllerContext = new ControllerContext(); ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext() { ModelBinderProviders = new ModelBinderProviderCollection(), ModelMetadata = GetMetadataForObject(new Person()), ModelName = "someName" }; Mock<IExtensibleModelBinder> mockDtoBinder = new Mock<IExtensibleModelBinder>(); mockDtoBinder .Expect(o => o.BindModel(controllerContext, It.IsAny<ExtensibleModelBindingContext>())) .Returns( delegate(ControllerContext cc, ExtensibleModelBindingContext mbc2) { return true; // just return the DTO unchanged }); bindingContext.ModelBinderProviders.RegisterBinderForType(typeof(ComplexModelDto), mockDtoBinder.Object, true /* suppressPrefixCheck */); Mock<TestableMutableObjectModelBinder> mockTestableBinder = new Mock<TestableMutableObjectModelBinder>() { CallBase = true }; mockTestableBinder.Expect(o => o.EnsureModelPublic(controllerContext, bindingContext)).Verifiable(); mockTestableBinder.Expect(o => o.GetMetadataForPropertiesPublic(controllerContext, bindingContext)).Returns(new ModelMetadata[0]).Verifiable(); TestableMutableObjectModelBinder testableBinder = mockTestableBinder.Object; testableBinder.MetadataProvider = new DataAnnotationsModelMetadataProvider(); // Act bool retValue = testableBinder.BindModel(controllerContext, bindingContext); // Assert Assert.IsTrue(retValue); Assert.IsInstanceOfType(bindingContext.Model, typeof(Person)); Assert.IsTrue(bindingContext.ValidationNode.ValidateAllProperties); mockTestableBinder.Verify(); }
public void ModelNameProperty() { // Arrange ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext(); // Act & assert Assert.Reflection.StringProperty(bindingContext, (context) => context.ModelName, String.Empty); }
public void ModelStateProperty() { // Arrange ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext(); ModelStateDictionary modelState = new ModelStateDictionary(); // Act & assert MemberHelper.TestPropertyWithDefaultInstance(bindingContext, "ModelState", modelState); }
public void ModelProperty() { // Arrange ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext() { ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(int)) }; // Act & assert MemberHelper.TestPropertyValue(bindingContext, "Model", 42); }
public void ModelProperty_ThrowsIfModelMetadataDoesNotExist() { // Arrange ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext(); // Act & assert Assert.Throws<InvalidOperationException>( delegate { bindingContext.Model = null; }, "The ModelMetadata property must be set before accessing this property."); }
public void GetBinder_NoPrefixInValueProvider_ReturnsNull() { // Arrange ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext() { ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(() => 42, typeof(int)), ModelName = "foo", ValueProvider = new SimpleValueProvider() }; MutableObjectModelBinderProvider binderProvider = new MutableObjectModelBinderProvider(); // Act IExtensibleModelBinder binder = binderProvider.GetBinder(null, bindingContext); // Assert Assert.IsNull(binder); }
public void GetBinder_ValueProviderDoesNotContainPrefix_ReturnsNull() { // Arrange ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext() { ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(IEnumerable<int>)), ModelName = "foo", ValueProvider = new SimpleValueProvider() }; CollectionModelBinderProvider binderProvider = new CollectionModelBinderProvider(); // Act IExtensibleModelBinder binder = binderProvider.GetBinder(null, bindingContext); // Assert Assert.IsNull(binder); }
public void GetBinder_ValueProviderDoesNotContainPrefix_ReturnsNull() { // Arrange ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext { ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(IDictionary<int, string>)), ModelName = "foo", ValueProvider = new SimpleValueProvider() }; DictionaryModelBinderProvider binderProvider = new DictionaryModelBinderProvider(); // Act IExtensibleModelBinder binder = binderProvider.GetBinder(null, bindingContext); // Assert Assert.Null(binder); }
public void GetBinder_TypeIsComplexModelDto_ReturnsNull() { // Arrange ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext() { ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(ComplexModelDto)), ModelName = "foo", ValueProvider = new SimpleValueProvider() { { "foo.bar", "someValue" } } }; MutableObjectModelBinderProvider binderProvider = new MutableObjectModelBinderProvider(); // Act IExtensibleModelBinder binder = binderProvider.GetBinder(null, bindingContext); // Assert Assert.IsNull(binder); }
public void CopyConstructor() { // Arrange ExtensibleModelBindingContext originalBindingContext = new ExtensibleModelBindingContext() { ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(object)), ModelName = "theName", ModelState = new ModelStateDictionary(), ValueProvider = new SimpleValueProvider() }; // Act ExtensibleModelBindingContext newBindingContext = new ExtensibleModelBindingContext(originalBindingContext); // Assert Assert.IsNull(newBindingContext.ModelMetadata, "Property 'ModelMetadata' should not have been propagated."); Assert.AreEqual("", newBindingContext.ModelName, "Property 'ModelName' should not have been propagated."); Assert.AreEqual(originalBindingContext.ModelState, newBindingContext.ModelState, "Property 'ModelState' should have been propagated."); Assert.AreEqual(originalBindingContext.ValueProvider, newBindingContext.ValueProvider, "Property 'ValueProvider' should have been propagated."); }
public void GetBinder_ModelTypeIsIncorrect_ReturnsNull() { // Arrange ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext() { ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(int)), ModelName = "foo", ValueProvider = new SimpleValueProvider() { { "foo[0]", "42" }, } }; CollectionModelBinderProvider binderProvider = new CollectionModelBinderProvider(); // Act IExtensibleModelBinder binder = binderProvider.GetBinder(null, bindingContext); // Assert Assert.IsNull(binder); }
public void GetBinder_CorrectModelTypeAndValueProviderEntries_ReturnsBinder() { // Arrange ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext() { ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(IEnumerable<int>)), ModelName = "foo", ValueProvider = new SimpleValueProvider() { { "foo[0]", "42" }, } }; CollectionModelBinderProvider binderProvider = new CollectionModelBinderProvider(); // Act IExtensibleModelBinder binder = binderProvider.GetBinder(null, bindingContext); // Assert Assert.IsInstanceOfType(binder, typeof(CollectionModelBinder<int>)); }
public void GetBinder_ValueProviderDoesNotContainValueProperty_ReturnsNull() { // Arrange ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext() { ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(KeyValuePair<int, string>)), ModelName = "foo", ValueProvider = new SimpleValueProvider() { { "foo.key", 42 } } }; KeyValuePairModelBinderProvider binderProvider = new KeyValuePairModelBinderProvider(); // Act IExtensibleModelBinder binder = binderProvider.GetBinder(null, bindingContext); // Assert Assert.IsNull(binder); }
public void GetBinder_ModelMetadataReturnsReadOnly_ReturnsNull() { // Arrange ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext() { ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(int[])), ModelName = "foo", ValueProvider = new SimpleValueProvider() { { "foo[0]", "42" }, } }; bindingContext.ModelMetadata.IsReadOnly = true; ArrayModelBinderProvider binderProvider = new ArrayModelBinderProvider(); // Act IExtensibleModelBinder binder = binderProvider.GetBinder(null, bindingContext); // Assert Assert.IsNull(binder); }
public void GetBinder_PrefixInValueProvider_ReturnsBinder() { // Arrange ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext() { ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(() => 42, typeof(int)), ModelName = "foo", ValueProvider = new SimpleValueProvider() { { "foo.bar", "someValue" } } }; MutableObjectModelBinderProvider binderProvider = new MutableObjectModelBinderProvider(); // Act IExtensibleModelBinder binder = binderProvider.GetBinder(null, bindingContext); // Assert Assert.IsNotNull(binder); Assert.AreEqual(typeof(MutableObjectModelBinder), binder.GetType()); }
public void GetBinder_CorrectModelTypeAndValueProviderEntries_ReturnsBinder() { // Arrange ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext() { ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(KeyValuePair<int, string>)), ModelName = "foo", ValueProvider = new SimpleValueProvider() { { "foo.key", 42 }, { "foo.value", "someValue" } } }; KeyValuePairModelBinderProvider binderProvider = new KeyValuePairModelBinderProvider(); // Act IExtensibleModelBinder binder = binderProvider.GetBinder(null, bindingContext); // Assert Assert.IsInstanceOfType(binder, typeof(KeyValuePairModelBinder<int, string>)); }
public void BindModel_EmptyValue_Fails() { // Arrange ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext() { ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(byte[])), ModelName = "foo", ValueProvider = new SimpleValueProvider() { { "foo", "" } } }; BinaryDataModelBinderProvider binderProvider = new BinaryDataModelBinderProvider(); // Act IExtensibleModelBinder binder = binderProvider.GetBinder(null, bindingContext); bool retVal = binder.BindModel(null, bindingContext); // Assert Assert.IsFalse(retVal); }
public void BindModel_SubBindingSucceeds() { // Arrange ControllerContext controllerContext = new ControllerContext(); ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext { ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(KeyValuePair<int, string>)), ModelName = "someName", ModelBinderProviders = new ModelBinderProviderCollection(), ValueProvider = new SimpleValueProvider() }; Mock<IExtensibleModelBinder> mockIntBinder = new Mock<IExtensibleModelBinder>(); mockIntBinder .Setup(o => o.BindModel(controllerContext, It.IsAny<ExtensibleModelBindingContext>())) .Returns( delegate(ControllerContext cc, ExtensibleModelBindingContext mbc) { mbc.Model = 42; return true; }); bindingContext.ModelBinderProviders.RegisterBinderForType(typeof(int), mockIntBinder.Object, true /* suppressPrefixCheck */); Mock<IExtensibleModelBinder> mockStringBinder = new Mock<IExtensibleModelBinder>(); mockStringBinder .Setup(o => o.BindModel(controllerContext, It.IsAny<ExtensibleModelBindingContext>())) .Returns( delegate(ControllerContext cc, ExtensibleModelBindingContext mbc) { mbc.Model = "forty-two"; return true; }); bindingContext.ModelBinderProviders.RegisterBinderForType(typeof(string), mockStringBinder.Object, true /* suppressPrefixCheck */); KeyValuePairModelBinder<int, string> binder = new KeyValuePairModelBinder<int, string>(); // Act bool retVal = binder.BindModel(controllerContext, bindingContext); // Assert Assert.True(retVal); Assert.Equal(new KeyValuePair<int, string>(42, "forty-two"), bindingContext.Model); Assert.Equal(new[] { "someName.key", "someName.value" }, bindingContext.ValidationNode.ChildNodes.Select(n => n.ModelStateKey).ToArray()); }
public void GetBinder_ModelTypeIsIncorrect_ReturnsNull() { // Arrange ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext() { ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(List<int>)), ModelName = "foo", ValueProvider = new SimpleValueProvider() { { "foo.key", 42 }, { "foo.value", "someValue" } } }; KeyValuePairModelBinderProvider binderProvider = new KeyValuePairModelBinderProvider(); // Act IExtensibleModelBinder binder = binderProvider.GetBinder(null, bindingContext); // Assert Assert.IsNull(binder); }
public void CopyConstructor() { // Arrange ExtensibleModelBindingContext originalBindingContext = new ExtensibleModelBindingContext { ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(object)), ModelName = "theName", ModelState = new ModelStateDictionary(), ValueProvider = new SimpleValueProvider() }; // Act ExtensibleModelBindingContext newBindingContext = new ExtensibleModelBindingContext(originalBindingContext); // Assert Assert.Null(newBindingContext.ModelMetadata); Assert.Equal("", newBindingContext.ModelName); Assert.Equal(originalBindingContext.ModelState, newBindingContext.ModelState); Assert.Equal(originalBindingContext.ValueProvider, newBindingContext.ValueProvider); }
public void BindModel_MissingKey_ReturnsFalse() { // Arrange ControllerContext controllerContext = new ControllerContext(); ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext() { ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(KeyValuePair<int, string>)), ModelName = "someName", ModelBinderProviders = new ModelBinderProviderCollection(), ValueProvider = new SimpleValueProvider() }; KeyValuePairModelBinder<int, string> binder = new KeyValuePairModelBinder<int, string>(); // Act bool retVal = binder.BindModel(controllerContext, bindingContext); // Assert Assert.IsFalse(retVal); Assert.IsNull(bindingContext.Model); Assert.AreEqual(0, bindingContext.ValidationNode.ChildNodes.Count, "No child nodes should have been added."); }
public void BindModel_SubBindingSucceeds() { // Arrange ControllerContext controllerContext = new ControllerContext(); ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext { ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType( null, typeof(KeyValuePair <int, string>) ), ModelName = "someName", ModelBinderProviders = new ModelBinderProviderCollection(), ValueProvider = new SimpleValueProvider() }; Mock <IExtensibleModelBinder> mockIntBinder = new Mock <IExtensibleModelBinder>(); mockIntBinder .Setup( o => o.BindModel(controllerContext, It.IsAny <ExtensibleModelBindingContext>()) ) .Returns( delegate(ControllerContext cc, ExtensibleModelBindingContext mbc) { mbc.Model = 42; return(true); } ); bindingContext.ModelBinderProviders.RegisterBinderForType( typeof(int), mockIntBinder.Object, true /* suppressPrefixCheck */ ); Mock <IExtensibleModelBinder> mockStringBinder = new Mock <IExtensibleModelBinder>(); mockStringBinder .Setup( o => o.BindModel(controllerContext, It.IsAny <ExtensibleModelBindingContext>()) ) .Returns( delegate(ControllerContext cc, ExtensibleModelBindingContext mbc) { mbc.Model = "forty-two"; return(true); } ); bindingContext.ModelBinderProviders.RegisterBinderForType( typeof(string), mockStringBinder.Object, true /* suppressPrefixCheck */ ); KeyValuePairModelBinder <int, string> binder = new KeyValuePairModelBinder <int, string>(); // Act bool retVal = binder.BindModel(controllerContext, bindingContext); // Assert Assert.True(retVal); Assert.Equal(new KeyValuePair <int, string>(42, "forty-two"), bindingContext.Model); Assert.Equal( new[] { "someName.key", "someName.value" }, bindingContext.ValidationNode.ChildNodes.Select(n => n.ModelStateKey).ToArray() ); }
public void BindModel() { // Arrange ControllerContext controllerContext = new ControllerContext(); MyModel model = new MyModel(); ModelMetadata modelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(() => model, typeof(MyModel)); ComplexModelDto dto = new ComplexModelDto(modelMetadata, modelMetadata.Properties); Mock <IExtensibleModelBinder> mockStringBinder = new Mock <IExtensibleModelBinder>(); mockStringBinder .Setup(b => b.BindModel(controllerContext, It.IsAny <ExtensibleModelBindingContext>())) .Returns( delegate(ControllerContext cc, ExtensibleModelBindingContext mbc) { Assert.Equal(typeof(string), mbc.ModelType); Assert.Equal("theModel.StringProperty", mbc.ModelName); mbc.ValidationNode = new ModelValidationNode(mbc.ModelMetadata, "theModel.StringProperty"); mbc.Model = "someStringValue"; return(true); }); Mock <IExtensibleModelBinder> mockIntBinder = new Mock <IExtensibleModelBinder>(); mockIntBinder .Setup(b => b.BindModel(controllerContext, It.IsAny <ExtensibleModelBindingContext>())) .Returns( delegate(ControllerContext cc, ExtensibleModelBindingContext mbc) { Assert.Equal(typeof(int), mbc.ModelType); Assert.Equal("theModel.IntProperty", mbc.ModelName); mbc.ValidationNode = new ModelValidationNode(mbc.ModelMetadata, "theModel.IntProperty"); mbc.Model = 42; return(true); }); Mock <IExtensibleModelBinder> mockDateTimeBinder = new Mock <IExtensibleModelBinder>(); mockDateTimeBinder .Setup(b => b.BindModel(controllerContext, It.IsAny <ExtensibleModelBindingContext>())) .Returns( delegate(ControllerContext cc, ExtensibleModelBindingContext mbc) { Assert.Equal(typeof(DateTime), mbc.ModelType); Assert.Equal("theModel.DateTimeProperty", mbc.ModelName); return(false); }); ModelBinderProviderCollection binders = new ModelBinderProviderCollection(); binders.RegisterBinderForType(typeof(string), mockStringBinder.Object, true /* suppressPrefixCheck */); binders.RegisterBinderForType(typeof(int), mockIntBinder.Object, true /* suppressPrefixCheck */); binders.RegisterBinderForType(typeof(DateTime), mockDateTimeBinder.Object, true /* suppressPrefixCheck */); ExtensibleModelBindingContext parentBindingContext = new ExtensibleModelBindingContext { ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(() => dto, typeof(ComplexModelDto)), ModelName = "theModel", ModelBinderProviders = binders }; ComplexModelDtoModelBinder binder = new ComplexModelDtoModelBinder(); // Act bool retVal = binder.BindModel(controllerContext, parentBindingContext); // Assert Assert.True(retVal); Assert.Equal(dto, parentBindingContext.Model); ComplexModelDtoResult stringDtoResult = dto.Results[dto.PropertyMetadata.Where(m => m.ModelType == typeof(string)).First()]; Assert.Equal("someStringValue", stringDtoResult.Model); Assert.Equal("theModel.StringProperty", stringDtoResult.ValidationNode.ModelStateKey); ComplexModelDtoResult intDtoResult = dto.Results[dto.PropertyMetadata.Where(m => m.ModelType == typeof(int)).First()]; Assert.Equal(42, intDtoResult.Model); Assert.Equal("theModel.IntProperty", intDtoResult.ValidationNode.ModelStateKey); ComplexModelDtoResult dateTimeDtoResult = dto.Results[dto.PropertyMetadata.Where(m => m.ModelType == typeof(DateTime)).First()]; Assert.Null(dateTimeDtoResult); }
public virtual object CreateModelPublic(ControllerContext controllerContext, ExtensibleModelBindingContext bindingContext) { return(base.CreateModel(controllerContext, bindingContext)); }
protected override object CreateModel(ControllerContext controllerContext, ExtensibleModelBindingContext bindingContext) { return(CreateModelPublic(controllerContext, bindingContext)); }
public virtual void EnsureModelPublic(ControllerContext controllerContext, ExtensibleModelBindingContext bindingContext) { base.EnsureModel(controllerContext, bindingContext); }
protected override void EnsureModel(ControllerContext controllerContext, ExtensibleModelBindingContext bindingContext) { EnsureModelPublic(controllerContext, bindingContext); }
public virtual IEnumerable <ModelMetadata> GetMetadataForPropertiesPublic(ControllerContext controllerContext, ExtensibleModelBindingContext bindingContext) { return(base.GetMetadataForProperties(controllerContext, bindingContext)); }
protected override IEnumerable <ModelMetadata> GetMetadataForProperties(ControllerContext controllerContext, ExtensibleModelBindingContext bindingContext) { return(GetMetadataForPropertiesPublic(controllerContext, bindingContext)); }
public virtual void SetPropertyPublic(ControllerContext controllerContext, ExtensibleModelBindingContext bindingContext, ModelMetadata propertyMetadata, ComplexModelDtoResult dtoResult) { base.SetProperty(controllerContext, bindingContext, propertyMetadata, dtoResult); }
protected override void SetProperty(ControllerContext controllerContext, ExtensibleModelBindingContext bindingContext, ModelMetadata propertyMetadata, ComplexModelDtoResult dtoResult) { SetPropertyPublic(controllerContext, bindingContext, propertyMetadata, dtoResult); }
public override IExtensibleModelBinder GetBinder(ControllerContext controllerContext, ExtensibleModelBindingContext bindingContext) { throw new NotImplementedException(); }
public override IExtensibleModelBinder GetBinder(ControllerContext controllerContext, ExtensibleModelBindingContext bindingContext) { return(new CustomBinder()); }
public bool BindModel(ControllerContext controllerContext, ExtensibleModelBindingContext bindingContext) { throw new NotImplementedException(); }