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);
        }
Ejemplo n.º 2
0
        public void SetProperty_ValueProvidedAndCanUpdatePropertyTrue_DoesNothing(
            string propertyName,
            Func<object, object> propertyAccessor)
        {
            // Arrange
            var model = new MyModelTestingCanUpdateProperty();
            var type = model.GetType();
            var bindingContext = CreateContext(GetMetadataForType(type), model);
            var modelState = bindingContext.ModelState;
            var metadata = GetMetadataForType(type);

            var propertyMetadata = bindingContext.ModelMetadata.Properties[propertyName];
            var result = ModelBindingResult.Success(new Simple { Name = "Hanna" });

            var binder = CreateBinder(bindingContext.ModelMetadata);

            // Act
            binder.SetPropertyPublic(bindingContext, propertyName, propertyMetadata, result);

            // Assert
            Assert.Equal("Joe", propertyAccessor(model));
            Assert.True(modelState.IsValid);
            Assert.Empty(modelState);
        }