本文整理汇总了C#中TestableMutableObjectModelBinder.SetPropertyPublic方法的典型用法代码示例。如果您正苦于以下问题:C# TestableMutableObjectModelBinder.SetPropertyPublic方法的具体用法?C# TestableMutableObjectModelBinder.SetPropertyPublic怎么用?C# TestableMutableObjectModelBinder.SetPropertyPublic使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类TestableMutableObjectModelBinder
的用法示例。
在下文中一共展示了TestableMutableObjectModelBinder.SetPropertyPublic方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SetProperty_PropertyIsReadOnly_DoesNothing
public void SetProperty_PropertyIsReadOnly_DoesNothing()
{
// Arrange
ModelBindingContext bindingContext = new ModelBindingContext
{
ModelMetadata = GetMetadataForType(typeof(Person))
};
ModelMetadata propertyMetadata = bindingContext.ModelMetadata.Properties.Single(o => o.PropertyName == "NonUpdateableProperty");
ModelValidationNode validationNode = new ModelValidationNode(propertyMetadata, "foo");
ComplexModelDtoResult dtoResult = new ComplexModelDtoResult(null /* model */, validationNode);
TestableMutableObjectModelBinder testableBinder = new TestableMutableObjectModelBinder();
// Act
testableBinder.SetPropertyPublic(null, bindingContext, propertyMetadata, dtoResult, requiredValidator: null);
// Assert
// If didn't throw, success!
}
示例2: SetProperty_PropertyIsSettable_CallsSetter
public void SetProperty_PropertyIsSettable_CallsSetter()
{
// Arrange
Person model = new Person();
HttpActionContext context = ContextUtil.CreateActionContext();
ModelBindingContext bindingContext = new ModelBindingContext
{
ModelMetadata = GetMetadataForObject(model)
};
ModelMetadata propertyMetadata = bindingContext.ModelMetadata.Properties.Single(o => o.PropertyName == "DateOfBirth");
ModelValidationNode validationNode = new ModelValidationNode(propertyMetadata, "foo");
ComplexModelDtoResult dtoResult = new ComplexModelDtoResult(new DateTime(2001, 1, 1), validationNode);
ModelValidator requiredValidator = context.GetValidators(propertyMetadata).Where(v => v.IsRequired).FirstOrDefault();
TestableMutableObjectModelBinder testableBinder = new TestableMutableObjectModelBinder();
// Act
testableBinder.SetPropertyPublic(context, bindingContext, propertyMetadata, dtoResult, requiredValidator);
// Assert
validationNode.Validate(context);
Assert.True(context.ModelState.IsValid);
Assert.Equal(new DateTime(2001, 1, 1), model.DateOfBirth);
}
示例3: EmptyController
public void SetProperty_SettingNullableTypeToNull_RequiredValidatorNotPresent_PropertySetterThrows_AddsRequiredMessageString()
{
// Arrange
ControllerContext controllerContext = new ControllerContext
{
Controller = new EmptyController()
};
ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext
{
ModelMetadata = GetMetadataForObject(new ModelWhosePropertySetterThrows()),
ModelName = "foo"
};
ModelMetadata propertyMetadata = bindingContext.ModelMetadata.Properties.Single(o => o.PropertyName == "NameNoAttribute");
ModelValidationNode validationNode = new ModelValidationNode(propertyMetadata, "foo.NameNoAttribute");
ComplexModelDtoResult dtoResult = new ComplexModelDtoResult(null /* model */, validationNode);
TestableMutableObjectModelBinder testableBinder = new TestableMutableObjectModelBinder();
// Act
testableBinder.SetPropertyPublic(controllerContext, bindingContext, propertyMetadata, dtoResult);
// 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);
}
示例4: SetProperty_PropertyHasDefaultValue_SetsDefaultValue
public void SetProperty_PropertyHasDefaultValue_SetsDefaultValue()
{
// Arrange
HttpActionContext context = ContextUtil.CreateActionContext();
ModelBindingContext bindingContext = new ModelBindingContext
{
ModelMetadata = GetMetadataForObject(new Person())
};
ModelMetadata propertyMetadata = bindingContext.ModelMetadata.Properties.Single(o => o.PropertyName == "PropertyWithDefaultValue");
ModelValidationNode validationNode = new ModelValidationNode(propertyMetadata, "foo");
ComplexModelDtoResult dtoResult = new ComplexModelDtoResult(null /* model */, validationNode);
ModelValidator requiredValidator = context.GetValidators(propertyMetadata).Where(v => v.IsRequired).FirstOrDefault();
TestableMutableObjectModelBinder testableBinder = new TestableMutableObjectModelBinder();
// Act
testableBinder.SetPropertyPublic(context, bindingContext, propertyMetadata, dtoResult, requiredValidator);
// Assert
var person = Assert.IsType<Person>(bindingContext.Model);
Assert.Equal(123.456m, person.PropertyWithDefaultValue);
Assert.True(context.ModelState.IsValid);
}
示例5: SetProperty_PropertyIsReadOnly_DoesNothing
public void SetProperty_PropertyIsReadOnly_DoesNothing()
{
// Arrange
var bindingContext = CreateContext(GetMetadataForObject(new Person()));
var propertyMetadata = bindingContext.ModelMetadata.Properties.Single(o => o.PropertyName == "NonUpdateableProperty");
var validationNode = new ModelValidationNode(propertyMetadata, "foo");
var dtoResult = new ComplexModelDtoResult(model: null, validationNode: validationNode);
var testableBinder = new TestableMutableObjectModelBinder();
// Act
testableBinder.SetPropertyPublic(bindingContext, propertyMetadata, dtoResult, requiredValidator: null);
// Assert
// If didn't throw, success!
}
示例6: SetProperty_SettingNonNullableValueTypeToNull_RequiredValidatorPresent_AddsModelError
public void SetProperty_SettingNonNullableValueTypeToNull_RequiredValidatorPresent_AddsModelError()
{
// Arrange
ControllerContext controllerContext = new ControllerContext
{
Controller = new EmptyController()
};
ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext
{
ModelMetadata = GetMetadataForObject(new Person()),
ModelName = "foo"
};
ModelMetadata propertyMetadata = bindingContext.ModelMetadata.Properties.Single(o => o.PropertyName == "ValueTypeRequired");
ModelValidationNode validationNode = new ModelValidationNode(propertyMetadata, "foo.ValueTypeRequired");
ComplexModelDtoResult dtoResult = new ComplexModelDtoResult(null /* model */, validationNode);
TestableMutableObjectModelBinder testableBinder = new TestableMutableObjectModelBinder();
// Act
testableBinder.SetPropertyPublic(controllerContext, bindingContext, propertyMetadata, dtoResult);
// Assert
Assert.False(bindingContext.ModelState.IsValid);
Assert.Equal("Sample message", bindingContext.ModelState["foo.ValueTypeRequired"].Errors[0].ErrorMessage);
}
示例7: SetProperty_PropertyHasDefaultValue_SetsDefaultValue
public void SetProperty_PropertyHasDefaultValue_SetsDefaultValue()
{
// Arrange
ControllerContext controllerContext = new ControllerContext
{
Controller = new EmptyController()
};
ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext
{
ModelMetadata = GetMetadataForObject(new Person())
};
ModelMetadata propertyMetadata = bindingContext.ModelMetadata.Properties.Single(o => o.PropertyName == "PropertyWithDefaultValue");
ModelValidationNode validationNode = new ModelValidationNode(propertyMetadata, "foo");
ComplexModelDtoResult dtoResult = new ComplexModelDtoResult(null /* model */, validationNode);
TestableMutableObjectModelBinder testableBinder = new TestableMutableObjectModelBinder();
// Act
testableBinder.SetPropertyPublic(controllerContext, bindingContext, propertyMetadata, dtoResult);
// Assert
var person = Assert.IsType<Person>(bindingContext.Model);
Assert.Equal(123.456m, person.PropertyWithDefaultValue);
Assert.True(controllerContext.Controller.ViewData.ModelState.IsValid);
}
示例8: SetProperty_SettingNonNullableValueTypeToNull_RequiredValidatorPresent_AddsModelError
public void SetProperty_SettingNonNullableValueTypeToNull_RequiredValidatorPresent_AddsModelError()
{
// Arrange
HttpActionContext context = ContextUtil.CreateActionContext();
ModelBindingContext bindingContext = new ModelBindingContext
{
ModelMetadata = GetMetadataForObject(new Person()),
ModelName = "foo"
};
ModelMetadata propertyMetadata = bindingContext.ModelMetadata.Properties.Single(o => o.PropertyName == "ValueTypeRequired");
ModelValidationNode validationNode = new ModelValidationNode(propertyMetadata, "foo.ValueTypeRequired");
ComplexModelDtoResult dtoResult = new ComplexModelDtoResult(null /* model */, validationNode);
ModelValidator requiredValidator = context.GetValidators(propertyMetadata).Where(v => v.IsRequired).FirstOrDefault();
TestableMutableObjectModelBinder testableBinder = new TestableMutableObjectModelBinder();
// Act
testableBinder.SetPropertyPublic(context, bindingContext, propertyMetadata, dtoResult, requiredValidator);
// Assert
Assert.False(bindingContext.ModelState.IsValid);
Assert.Equal("Sample message", bindingContext.ModelState["foo.ValueTypeRequired"].Errors[0].ErrorMessage);
}
示例9: CreateContext
public void SetProperty_SettingNullableTypeToNull_RequiredValidatorPresent_PropertySetterThrows_AddsRequiredMessageString()
{
// Arrange
var bindingContext = CreateContext(GetMetadataForObject(new ModelWhosePropertySetterThrows()));
bindingContext.ModelName = "foo";
var propertyMetadata = bindingContext.ModelMetadata.Properties.Single(o => o.PropertyName == "Name");
var validationNode = new ModelValidationNode(propertyMetadata, "foo.Name");
var dtoResult = new ComplexModelDtoResult(model: null, validationNode: validationNode);
var requiredValidator = GetRequiredValidator(bindingContext, propertyMetadata);
var testableBinder = new TestableMutableObjectModelBinder();
// Act
testableBinder.SetPropertyPublic(bindingContext, propertyMetadata, dtoResult, requiredValidator);
// Assert
Assert.Equal(false, bindingContext.ModelState.IsValid);
Assert.Equal(1, bindingContext.ModelState["foo.Name"].Errors.Count);
Assert.Equal("This message comes from the [Required] attribute.", bindingContext.ModelState["foo.Name"].Errors[0].ErrorMessage);
}
示例10: ControllerContext
public void SetProperty_SettingNullableTypeToNull_RequiredValidatorPresent_PropertySetterThrows_AddsRequiredMessageString() {
// Arrange
ControllerContext controllerContext = new ControllerContext() {
Controller = new EmptyController()
};
ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext() {
ModelMetadata = GetMetadataForObject(new ModelWhosePropertySetterThrows()),
ModelName = "foo"
};
ModelMetadata propertyMetadata = bindingContext.ModelMetadata.Properties.Single(o => o.PropertyName == "Name");
ModelValidationNode validationNode = new ModelValidationNode(propertyMetadata, "foo.Name");
ComplexModelDtoResult dtoResult = new ComplexModelDtoResult(null /* model */, validationNode);
TestableMutableObjectModelBinder testableBinder = new TestableMutableObjectModelBinder();
// Act
testableBinder.SetPropertyPublic(controllerContext, bindingContext, propertyMetadata, dtoResult);
// Assert
Assert.IsFalse(bindingContext.ModelState.IsValid, "ModelState should've been marked invalid.");
Assert.AreEqual(1, bindingContext.ModelState["foo.Name"].Errors.Count);
Assert.AreEqual("This message comes from the [Required] attribute.", bindingContext.ModelState["foo.Name"].Errors[0].ErrorMessage);
}
示例11: SetProperty_SettingNonNullableValueTypeToNull_RequiredValidatorPresent_AddsModelError
public void SetProperty_SettingNonNullableValueTypeToNull_RequiredValidatorPresent_AddsModelError()
{
// Arrange
var bindingContext = CreateContext(GetMetadataForObject(new Person()));
bindingContext.ModelName = " foo";
var propertyMetadata = bindingContext.ModelMetadata.Properties.Single(o => o.PropertyName == "ValueTypeRequired");
var validationNode = new ModelValidationNode(propertyMetadata, "foo.ValueTypeRequired");
var dtoResult = new ComplexModelDtoResult(model: null, validationNode: validationNode);
var requiredValidator = GetRequiredValidator(bindingContext, propertyMetadata);
var testableBinder = new TestableMutableObjectModelBinder();
// Act
testableBinder.SetPropertyPublic(bindingContext, propertyMetadata, dtoResult, requiredValidator);
// Assert
Assert.Equal(false, bindingContext.ModelState.IsValid);
Assert.Equal("Sample message", bindingContext.ModelState["foo.ValueTypeRequired"].Errors[0].ErrorMessage);
}
示例12: SetProperty_SettingNonNullableValueTypeToNull_RequiredValidatorNotPresent_RegistersValidationCallback
public void SetProperty_SettingNonNullableValueTypeToNull_RequiredValidatorNotPresent_RegistersValidationCallback()
{
// Arrange
var bindingContext = CreateContext(GetMetadataForObject(new Person()));
var propertyMetadata = bindingContext.ModelMetadata.Properties.Single(o => o.PropertyName == "DateOfBirth");
var validationNode = new ModelValidationNode(propertyMetadata, "foo");
var dtoResult = new ComplexModelDtoResult(model: null, validationNode: validationNode);
var requiredValidator = GetRequiredValidator(bindingContext, propertyMetadata);
var validationContext = new ModelValidationContext(bindingContext, propertyMetadata);
var testableBinder = new TestableMutableObjectModelBinder();
// Act
testableBinder.SetPropertyPublic(bindingContext, propertyMetadata, dtoResult, requiredValidator);
// Assert
Assert.Equal(true, bindingContext.ModelState.IsValid);
validationNode.Validate(validationContext, bindingContext.ValidationNode);
Assert.Equal(false, bindingContext.ModelState.IsValid);
}
示例13: SetProperty_PropertyIsSettable_CallsSetter
public void SetProperty_PropertyIsSettable_CallsSetter()
{
// Arrange
var model = new Person();
var bindingContext = CreateContext(GetMetadataForObject(model));
var propertyMetadata = bindingContext.ModelMetadata.Properties.Single(o => o.PropertyName == "DateOfBirth");
var validationNode = new ModelValidationNode(propertyMetadata, "foo");
var dtoResult = new ComplexModelDtoResult(new DateTime(2001, 1, 1), validationNode);
var requiredValidator = bindingContext.ValidatorProviders
.SelectMany(v => v.GetValidators(propertyMetadata))
.Where(v => v.IsRequired)
.FirstOrDefault();
var validationContext = new ModelValidationContext(bindingContext, propertyMetadata);
var testableBinder = new TestableMutableObjectModelBinder();
// Act
testableBinder.SetPropertyPublic(bindingContext, propertyMetadata, dtoResult, requiredValidator);
// Assert
validationNode.Validate(validationContext);
Assert.Equal(true, bindingContext.ModelState.IsValid);
Assert.Equal(new DateTime(2001, 1, 1), model.DateOfBirth);
}
示例14: SetProperty_PropertyIsSettable_SetterThrows_RecordsError
public void SetProperty_PropertyIsSettable_SetterThrows_RecordsError()
{
// Arrange
Person model = new Person
{
DateOfBirth = new DateTime(1900, 1, 1)
};
ModelBindingContext bindingContext = new ModelBindingContext
{
ModelMetadata = GetMetadataForObject(model)
};
ModelMetadata propertyMetadata = bindingContext.ModelMetadata.Properties.Single(o => o.PropertyName == "DateOfDeath");
ModelValidationNode validationNode = new ModelValidationNode(propertyMetadata, "foo");
ComplexModelDtoResult dtoResult = new ComplexModelDtoResult(new DateTime(1800, 1, 1), validationNode);
TestableMutableObjectModelBinder testableBinder = new TestableMutableObjectModelBinder();
// Act
testableBinder.SetPropertyPublic(null, bindingContext, propertyMetadata, dtoResult, requiredValidator: null);
// 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);
}
示例15: SetProperty_PropertyIsSettable_CallsSetter
public void SetProperty_PropertyIsSettable_CallsSetter()
{
// Arrange
Person model = new Person();
ControllerContext controllerContext = new ControllerContext
{
Controller = new EmptyController()
};
ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext
{
ModelMetadata = GetMetadataForObject(model)
};
ModelMetadata propertyMetadata = bindingContext.ModelMetadata.Properties.Single(o => o.PropertyName == "DateOfBirth");
ModelValidationNode validationNode = new ModelValidationNode(propertyMetadata, "foo");
ComplexModelDtoResult dtoResult = new ComplexModelDtoResult(new DateTime(2001, 1, 1), validationNode);
TestableMutableObjectModelBinder testableBinder = new TestableMutableObjectModelBinder();
// Act
testableBinder.SetPropertyPublic(controllerContext, bindingContext, propertyMetadata, dtoResult);
// Assert
validationNode.Validate(controllerContext);
Assert.True(controllerContext.Controller.ViewData.ModelState.IsValid);
Assert.Equal(new DateTime(2001, 1, 1), model.DateOfBirth);
}