当前位置: 首页>>代码示例>>C#>>正文


C# TestableMutableObjectModelBinder.SetProperty方法代码示例

本文整理汇总了C#中TestableMutableObjectModelBinder.SetProperty方法的典型用法代码示例。如果您正苦于以下问题:C# TestableMutableObjectModelBinder.SetProperty方法的具体用法?C# TestableMutableObjectModelBinder.SetProperty怎么用?C# TestableMutableObjectModelBinder.SetProperty使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在TestableMutableObjectModelBinder的用法示例。


在下文中一共展示了TestableMutableObjectModelBinder.SetProperty方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: SetProperty_PropertySetterThrows_CapturesException

        public void SetProperty_PropertySetterThrows_CapturesException()
        {
            // Arrange
            var model = new ModelWhosePropertySetterThrows();
            var bindingContext = CreateContext(GetMetadataForType(model.GetType()), model);
            bindingContext.ModelName = "foo";

            var metadataProvider = bindingContext.OperationBindingContext.MetadataProvider;
            var modelExplorer = metadataProvider.GetModelExplorerForType(typeof(ModelWhosePropertySetterThrows), model);
            var propertyMetadata = bindingContext.ModelMetadata.Properties[nameof(model.NameNoAttribute)];

            var result = new ModelBindingResult(model: null, isModelSet: true, key: "foo.NameNoAttribute");
            var testableBinder = new TestableMutableObjectModelBinder();

            // Act
            testableBinder.SetProperty(bindingContext, modelExplorer, propertyMetadata, result);

            // Assert
            Assert.False(bindingContext.ModelState.IsValid);
            Assert.Equal(1, bindingContext.ModelState["foo.NameNoAttribute"].Errors.Count);
            Assert.Equal("This is a different exception." + Environment.NewLine
                       + "Parameter name: value",
                         bindingContext.ModelState["foo.NameNoAttribute"].Errors[0].Exception.Message);
        }
开发者ID:ryanbrandenburg,项目名称:Mvc,代码行数:24,代码来源:MutableObjectModelBinderTest.cs

示例2: SetProperty_PropertyIsSettable_SetterThrows_RecordsError

        public void SetProperty_PropertyIsSettable_SetterThrows_RecordsError()
        {
            // Arrange
            var model = new Person
            {
                DateOfBirth = new DateTime(1900, 1, 1)
            };

            var bindingContext = CreateContext(GetMetadataForType(model.GetType()), model);

            var metadataProvider = bindingContext.OperationBindingContext.MetadataProvider;
            var modelExplorer = metadataProvider.GetModelExplorerForType(typeof(Person), model);
            var propertyMetadata = bindingContext.ModelMetadata.Properties[nameof(model.DateOfDeath)];

            var result = new ModelBindingResult(new DateTime(1800, 1, 1), isModelSet: true, key: "foo");
            var testableBinder = new TestableMutableObjectModelBinder();

            // Act
            testableBinder.SetProperty(bindingContext, modelExplorer, propertyMetadata, result);

            // Assert
            Assert.Equal("Date of death can't be before date of birth." + Environment.NewLine
                       + "Parameter name: value",
                         bindingContext.ModelState["foo"].Errors[0].Exception.Message);
        }
开发者ID:ryanbrandenburg,项目名称:Mvc,代码行数:25,代码来源:MutableObjectModelBinderTest.cs

示例3: SetProperty_SettingNonNullableValueTypeToNull_CapturesException

        public void SetProperty_SettingNonNullableValueTypeToNull_CapturesException()
        {
            // Arrange
            var model = new Person();
            var bindingContext = CreateContext(GetMetadataForType(model.GetType()), model);

            var metadataProvider = bindingContext.OperationBindingContext.MetadataProvider;
            var modelExplorer = metadataProvider.GetModelExplorerForType(typeof(Person), model);
            var propertyMetadata = bindingContext.ModelMetadata.Properties[nameof(model.DateOfBirth)];

            var result = new ModelBindingResult(model: null, isModelSet: true, key: "foo.DateOfBirth");
            var testableBinder = new TestableMutableObjectModelBinder();

            // Act
            testableBinder.SetProperty(bindingContext, modelExplorer, propertyMetadata, result);

            // Assert
            Assert.False(bindingContext.ModelState.IsValid);

            var entry = Assert.Single(bindingContext.ModelState, kvp => kvp.Key == "foo.DateOfBirth").Value;
            var error = Assert.Single(entry.Errors);
            Assert.Equal(string.Empty, error.ErrorMessage);
            Assert.IsType<NullReferenceException>(error.Exception);
        }
开发者ID:ryanbrandenburg,项目名称:Mvc,代码行数:24,代码来源:MutableObjectModelBinderTest.cs

示例4: SetProperty_CollectionProperty_UpdatesModel

        public void SetProperty_CollectionProperty_UpdatesModel(
            string propertyName,
            Func<object, object> propertyAccessor,
            object collection)
        {
            // Arrange
            var model = new CollectionContainer();
            var type = model.GetType();
            var bindingContext = CreateContext(GetMetadataForType(type), model);
            var modelState = bindingContext.ModelState;
            var metadataProvider = bindingContext.OperationBindingContext.MetadataProvider;
            var modelExplorer = metadataProvider.GetModelExplorerForType(type, model);

            var propertyMetadata = bindingContext.ModelMetadata.Properties[propertyName];
            var result = new ModelBindingResult(model: collection, isModelSet: true, key: propertyName);
            var testableBinder = new TestableMutableObjectModelBinder();

            // Act
            testableBinder.SetProperty(bindingContext, modelExplorer, propertyMetadata, result);

            // Assert
            Assert.Equal(collection, propertyAccessor(model));
            Assert.True(modelState.IsValid);
            Assert.Empty(modelState);
        }
开发者ID:ryanbrandenburg,项目名称:Mvc,代码行数:25,代码来源:MutableObjectModelBinderTest.cs

示例5: SetProperty_PropertyIsSettable_CallsSetter

        public void SetProperty_PropertyIsSettable_CallsSetter()
        {
            // Arrange
            var model = new Person();
            var bindingContext = CreateContext(GetMetadataForType(model.GetType()), model);

            var metadataProvider = bindingContext.OperationBindingContext.MetadataProvider;
            var modelExplorer = metadataProvider.GetModelExplorerForType(typeof(Person), model);
            var propertyMetadata = bindingContext.ModelMetadata.Properties[nameof(model.DateOfBirth)];

            var result = new ModelBindingResult(new DateTime(2001, 1, 1), key: "foo", isModelSet: true);
            var testableBinder = new TestableMutableObjectModelBinder();

            // Act
            testableBinder.SetProperty(bindingContext, modelExplorer, propertyMetadata, result);

            // Assert
            Assert.True(bindingContext.ModelState.IsValid);
            Assert.Equal(new DateTime(2001, 1, 1), model.DateOfBirth);
        }
开发者ID:ryanbrandenburg,项目名称:Mvc,代码行数:20,代码来源:MutableObjectModelBinderTest.cs

示例6: SetProperty_PropertyIsReadOnly_DoesNothing

        public void SetProperty_PropertyIsReadOnly_DoesNothing()
        {
            // Arrange
            var model = new Person();
            var bindingContext = CreateContext(GetMetadataForType(model.GetType()), model);

            var metadataProvider = bindingContext.OperationBindingContext.MetadataProvider;
            var modelExplorer = metadataProvider.GetModelExplorerForType(typeof(Person), model);
            var propertyMetadata = bindingContext.ModelMetadata.Properties[nameof(model.NonUpdateableProperty)];

            var result = new ModelBindingResult(model: null, isModelSet: false, key: "foo");
            var testableBinder = new TestableMutableObjectModelBinder();

            // Act
            testableBinder.SetProperty(bindingContext, modelExplorer, propertyMetadata, result);

            // Assert
            // If didn't throw, success!
        }
开发者ID:ryanbrandenburg,项目名称:Mvc,代码行数:19,代码来源:MutableObjectModelBinderTest.cs

示例7: SetProperty_ValueProvidedAndCanUpdatePropertyTrue_DoesNothing

        public void SetProperty_ValueProvidedAndCanUpdatePropertyTrue_DoesNothing(
            string propertyName,
            Func<object, object> propertAccessor)
        {
            // Arrange
            var model = new MyModelTestingCanUpdateProperty();
            var type = model.GetType();
            var bindingContext = CreateContext(GetMetadataForType(type), model);
            var modelState = bindingContext.ModelState;
            var metadataProvider = bindingContext.OperationBindingContext.MetadataProvider;
            var modelExplorer = metadataProvider.GetModelExplorerForType(type, model);

            var propertyMetadata = bindingContext.ModelMetadata.Properties[propertyName];
            var result = new ModelBindingResult(
                model: new Simple { Name = "Hanna" },
                isModelSet: true,
                key: propertyName);

            var testableBinder = new TestableMutableObjectModelBinder();

            // Act
            testableBinder.SetProperty(bindingContext, modelExplorer, propertyMetadata, result);

            // Assert
            Assert.Equal("Joe", propertAccessor(model));
            Assert.True(modelState.IsValid);
            Assert.Empty(modelState);
        }
开发者ID:ryanbrandenburg,项目名称:Mvc,代码行数:28,代码来源:MutableObjectModelBinderTest.cs

示例8: SetProperty_PropertyHasDefaultValue_DefaultValueAttributeDoesNothing

        public void SetProperty_PropertyHasDefaultValue_DefaultValueAttributeDoesNothing()
        {
            // Arrange
            var model = new Person();
            var bindingContext = CreateContext(GetMetadataForType(model.GetType()), model);

            var metadataProvider = bindingContext.OperationBindingContext.MetadataProvider;
            var modelExplorer = metadataProvider.GetModelExplorerForType(typeof(Person), model);
            var propertyMetadata = bindingContext.ModelMetadata.Properties[nameof(model.PropertyWithDefaultValue)];

            var result = new ModelBindingResult(model: null, isModelSet: false, key: "foo");
            var testableBinder = new TestableMutableObjectModelBinder();

            // Act
            testableBinder.SetProperty(bindingContext, modelExplorer, propertyMetadata, result);

            // Assert
            var person = Assert.IsType<Person>(bindingContext.Model);
            Assert.Equal(0m, person.PropertyWithDefaultValue);
            Assert.True(bindingContext.ModelState.IsValid);
        }
开发者ID:ryanbrandenburg,项目名称:Mvc,代码行数:21,代码来源:MutableObjectModelBinderTest.cs

示例9: SetProperty_PropertyIsPreinitialized_DefaultValueAttributeDoesNothing

        public void SetProperty_PropertyIsPreinitialized_DefaultValueAttributeDoesNothing()
        {
            // Arrange
            var model = new Person();
            var bindingContext = CreateContext(GetMetadataForType(model.GetType()), model);

            var metadataProvider = bindingContext.OperationBindingContext.MetadataProvider;
            var modelExplorer = metadataProvider.GetModelExplorerForType(typeof(Person), model);
            var propertyMetadata =
                bindingContext.ModelMetadata.Properties[nameof(model.PropertyWithInitializedValueAndDefault)];

            // This value won't be used because IsModelBound = false.
            var result = new ModelBindingResult(model: "bad-value", isModelSet: false, key: "foo");

            var testableBinder = new TestableMutableObjectModelBinder();

            // Act
            testableBinder.SetProperty(bindingContext, modelExplorer, propertyMetadata, result);

            // Assert
            var person = Assert.IsType<Person>(bindingContext.Model);
            Assert.Equal("preinitialized", person.PropertyWithInitializedValueAndDefault);
            Assert.True(bindingContext.ModelState.IsValid);
        }
开发者ID:ryanbrandenburg,项目名称:Mvc,代码行数:24,代码来源:MutableObjectModelBinderTest.cs

示例10: ModelWhosePropertySetterThrows

        public void SetProperty_SettingNullableTypeToNull_RequiredValidatorPresent_PropertySetterThrows_AddsRequiredMessageString()
        {
            // Arrange
            var model = new ModelWhosePropertySetterThrows();
            var bindingContext = CreateContext(GetMetadataForType(model.GetType()), model);
            bindingContext.ModelName = "foo";

            var metadataProvider = bindingContext.OperationBindingContext.MetadataProvider;
            var modelExplorer = metadataProvider.GetModelExplorerForType(typeof(Person), model);
            var propertyMetadata = bindingContext.ModelMetadata.Properties["Name"];

            var dtoResult = new ModelBindingResult(
                model: null,
                isModelSet: true,
                key: "foo.Name");

            var requiredValidator = GetRequiredValidator(bindingContext, propertyMetadata);

            var testableBinder = new TestableMutableObjectModelBinder();

            // Act
            testableBinder.SetProperty(
                bindingContext,
                modelExplorer,
                propertyMetadata,
                dtoResult,
                requiredValidator);

            // Assert
            Assert.False(bindingContext.ModelState.IsValid);
            var error = Assert.Single(bindingContext.ModelState["foo.Name"].Errors);
            Assert.Equal("This message comes from the [Required] attribute.", error.ErrorMessage);
        }
开发者ID:AndersBillLinden,项目名称:Mvc,代码行数:33,代码来源:MutableObjectModelBinderTest.cs

示例11: SetProperty_PropertyIsPreinitialized_NoValue_DoesNothing

        public void SetProperty_PropertyIsPreinitialized_NoValue_DoesNothing()
        {
            // Arrange
            var model = new Person();
            var bindingContext = CreateContext(GetMetadataForType(model.GetType()), model);

            var metadataProvider = bindingContext.OperationBindingContext.MetadataProvider;
            var metadata = metadataProvider.GetMetadataForType(typeof(Person));
            var propertyMetadata = metadata.Properties[nameof(model.PropertyWithInitializedValue)];

            // The null model value won't be used because IsModelBound = false.
            var result = ModelBindingResult.Failed("foo");

            var testableBinder = new TestableMutableObjectModelBinder();

            // Act
            testableBinder.SetProperty(bindingContext, metadata, propertyMetadata, result);

            // Assert
            var person = Assert.IsType<Person>(bindingContext.Model);
            Assert.Equal("preinitialized", person.PropertyWithInitializedValue);
            Assert.True(bindingContext.ModelState.IsValid);
        }
开发者ID:huoxudong125,项目名称:Mvc,代码行数:23,代码来源:MutableObjectModelBinderTest.cs

示例12: SetProperty_SettingNonNullableValueTypeToNull_RequiredValidatorPresent_AddsModelError

        public void SetProperty_SettingNonNullableValueTypeToNull_RequiredValidatorPresent_AddsModelError()
        {
            // Arrange
            var model = new Person();
            var bindingContext = CreateContext(GetMetadataForType(model.GetType()), model);
            bindingContext.ModelName = " foo";

            var metadataProvider = bindingContext.OperationBindingContext.MetadataProvider;
            var modelExplorer = metadataProvider.GetModelExplorerForType(typeof(Person), model);
            var propertyMetadata = bindingContext.ModelMetadata.Properties["ValueTypeRequired"];

            var dtoResult = new ModelBindingResult(
                model: null,
                isModelSet: true,
                key: "foo.ValueTypeRequired");

            var requiredValidator = GetRequiredValidator(bindingContext, propertyMetadata);

            var testableBinder = new TestableMutableObjectModelBinder();

            // Act
            testableBinder.SetProperty(
                bindingContext,
                modelExplorer,
                propertyMetadata,
                dtoResult,
                requiredValidator);

            // Assert
            Assert.False(bindingContext.ModelState.IsValid);
            Assert.Equal("Sample message", bindingContext.ModelState["foo.ValueTypeRequired"].Errors[0].ErrorMessage);
        }
开发者ID:AndersBillLinden,项目名称:Mvc,代码行数:32,代码来源:MutableObjectModelBinderTest.cs

示例13: SetProperty_SettingNonNullableValueTypeToNull_RequiredValidatorNotPresent_RegistersValidationCallback

        public void SetProperty_SettingNonNullableValueTypeToNull_RequiredValidatorNotPresent_RegistersValidationCallback()
        {
            // Arrange
            var model = new Person();
            var bindingContext = CreateContext(GetMetadataForType(model.GetType()), model);

            var metadataProvider = bindingContext.OperationBindingContext.MetadataProvider;
            var modelExplorer = metadataProvider.GetModelExplorerForType(typeof(Person), model);
            var propertyMetadata = bindingContext.ModelMetadata.Properties["DateOfBirth"];

            var dtoResult = new ModelBindingResult(
                model: null,
                isModelSet: true,
                key: "foo");

            var requiredValidator = GetRequiredValidator(bindingContext, propertyMetadata);

            var testableBinder = new TestableMutableObjectModelBinder();

            // Act
            testableBinder.SetProperty(
                bindingContext,
                modelExplorer,
                propertyMetadata,
                dtoResult,
                requiredValidator);

            // Assert
            Assert.False(bindingContext.ModelState.IsValid);
        }
开发者ID:AndersBillLinden,项目名称:Mvc,代码行数:30,代码来源:MutableObjectModelBinderTest.cs

示例14: SetProperty_PropertyIsSettable_CallsSetter

        public void SetProperty_PropertyIsSettable_CallsSetter()
        {
            // Arrange
            var model = new Person();
            var bindingContext = CreateContext(GetMetadataForType(model.GetType()), model);

            var metadataProvider = bindingContext.OperationBindingContext.MetadataProvider;
            var modelExplorer = metadataProvider.GetModelExplorerForType(typeof(Person), model);
            var propertyMetadata = bindingContext.ModelMetadata.Properties["DateOfBirth"];

            var dtoResult = new ModelBindingResult(
                new DateTime(2001, 1, 1),
                key: "foo",
                isModelSet: true);

            var validatorProvider = bindingContext.OperationBindingContext.ValidatorProvider;
            var validatorProviderContext = new ModelValidatorProviderContext(propertyMetadata);
            validatorProvider.GetValidators(validatorProviderContext);

            var requiredValidator = validatorProviderContext.Validators.FirstOrDefault(v => v.IsRequired);

            var testableBinder = new TestableMutableObjectModelBinder();

            // Act
            testableBinder.SetProperty(
                bindingContext,
                modelExplorer,
                propertyMetadata,
                dtoResult,
                requiredValidator);

            // Assert
            Assert.True(bindingContext.ModelState.IsValid);
            Assert.Equal(new DateTime(2001, 1, 1), model.DateOfBirth);
        }
开发者ID:AndersBillLinden,项目名称:Mvc,代码行数:35,代码来源:MutableObjectModelBinderTest.cs

示例15: SetProperty_PropertyHasDefaultValue_SetsDefaultValue

        public void SetProperty_PropertyHasDefaultValue_SetsDefaultValue()
        {
            // Arrange
            var model = new Person();
            var bindingContext = CreateContext(GetMetadataForType(model.GetType()), model);

            var metadataProvider = bindingContext.OperationBindingContext.MetadataProvider;
            var modelExplorer = metadataProvider.GetModelExplorerForType(typeof(Person), model);
            var propertyMetadata = bindingContext.ModelMetadata.Properties["PropertyWithDefaultValue"];

            var dtoResult = new ModelBindingResult(
                model: null,
                isModelSet: false,
                key: "foo");

            var validatorProvider = bindingContext.OperationBindingContext.ValidatorProvider;
            var validatorProviderContext = new ModelValidatorProviderContext(propertyMetadata);
            validatorProvider.GetValidators(validatorProviderContext);

            var requiredValidator = validatorProviderContext.Validators.FirstOrDefault(v => v.IsRequired);

            var testableBinder = new TestableMutableObjectModelBinder();

            // Act
            testableBinder.SetProperty(
                bindingContext,
                modelExplorer,
                propertyMetadata,
                dtoResult,
                requiredValidator);

            // Assert
            var person = Assert.IsType<Person>(bindingContext.Model);
            Assert.Equal(123.456m, person.PropertyWithDefaultValue);
            Assert.True(bindingContext.ModelState.IsValid);
        }
开发者ID:AndersBillLinden,项目名称:Mvc,代码行数:36,代码来源:MutableObjectModelBinderTest.cs


注:本文中的TestableMutableObjectModelBinder.SetProperty方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。