测试依赖于另一个 属性 的验证属性
Testing a validation attribute that depends on another property
我创建了一个 ValidationAttribute
,它基本上检查另一个 属性 是否有值,如果有,属性 就变成可选的。鉴于此 属性 依赖于另一个 属性,我如何才能正确地模拟 属性,我假设 ValidationContext
OptionalIfAttribute
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
public class OptionalIfAttribute : ValidationAttribute
{
#region Constructor
private readonly string otherPropertyName;
public OptionalIfAttribute(string otherPropertyName)
{
this.otherPropertyName = otherPropertyName;
}
#endregion
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
var otherPropertyInfo = validationContext.ObjectType.GetProperty(this.otherPropertyName);
var otherPropertyValue = otherPropertyInfo.GetValue(validationContext.ObjectInstance, null);
if (value != null)
{
if (otherPropertyValue == null)
{
return new ValidationResult(FormatErrorMessage(this.ErrorMessage));
}
}
return ValidationResult.Success;
}
}
测试
[Test]
public void Should_BeValid_WhenPropertyIsNullAndOtherPropertyIsNull()
{
var attribute = new OptionalIfAttribute("OtherProperty");
var result = attribute.IsValid(null);
Assert.That(result, Is.True);
}
最简单的事情就是这样,
[Test]
public void Should_BeValid_WhenPropertyIsNullAndOtherPropertyIsNull()
{
var attribute = new OptionalIfAttribute("OtherProperty");
//**********************
var model = new testModel;//your model that you want to test the validation against
var context = new ValidationContext(testModel, null, null);
var result = attribute.IsValid(testModel, context);
Assert.That(result.Count == 0, Is.True); //is valid or Count > 0 not valid
}
这在没有具体模型的情况下进行测试class:
[TestMethod]
public void When_BothPropertiesAreSet_SuccessResult()
{
var mockModel = new Mock<ISomeModel>();
mockModel.Setup(m => m.SomeProperty).Returns("something");
var attribute = new OptionalIfAttribute("SomeProperty");
var context = new ValidationContext(mockModel.Object, null, null);
var result = attribute.IsValid(string.Empty, context);
Assert.AreEqual(ValidationResult.Success, result);
}
[TestMethod]
public void When_SecondPropertyIsNotSet_ErrorResult()
{
const string ExpectedErrorMessage = "Whoops!";
var mockModel = new Mock<ISomeModel>();
mockModel.Setup(m => m.SomeProperty).Returns((string)null);
var attribute = new OptionalIfAttribute("SomeProperty");
attribute.ErrorMessage = ExpectedErrorMessage;
var context = new ValidationContext(mockModel.Object, null, null);
var result = attribute.IsValid(string.Empty, context);
Assert.AreEqual(ExpectedErrorMessage, result.ErrorMessage);
}
我正在使用下面的代码来测试我的自定义验证器 class
[TestMethod]
public void IsValid_Test()
{
var modelObj = new youModelClass {
requiredProp = value
};
var validatorClassObj = new yourValidatorClass();
var validationResult = validatorClassObj.GetValidationResult( valueToValidate, new ValidationContext( modelObj ) );
Assert.AreEqual( ValidationResult.Success, validationResult );
}
很高兴知道是否还有其他测试方法。
我创建了一个 ValidationAttribute
,它基本上检查另一个 属性 是否有值,如果有,属性 就变成可选的。鉴于此 属性 依赖于另一个 属性,我如何才能正确地模拟 属性,我假设 ValidationContext
OptionalIfAttribute
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
public class OptionalIfAttribute : ValidationAttribute
{
#region Constructor
private readonly string otherPropertyName;
public OptionalIfAttribute(string otherPropertyName)
{
this.otherPropertyName = otherPropertyName;
}
#endregion
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
var otherPropertyInfo = validationContext.ObjectType.GetProperty(this.otherPropertyName);
var otherPropertyValue = otherPropertyInfo.GetValue(validationContext.ObjectInstance, null);
if (value != null)
{
if (otherPropertyValue == null)
{
return new ValidationResult(FormatErrorMessage(this.ErrorMessage));
}
}
return ValidationResult.Success;
}
}
测试
[Test]
public void Should_BeValid_WhenPropertyIsNullAndOtherPropertyIsNull()
{
var attribute = new OptionalIfAttribute("OtherProperty");
var result = attribute.IsValid(null);
Assert.That(result, Is.True);
}
最简单的事情就是这样,
[Test]
public void Should_BeValid_WhenPropertyIsNullAndOtherPropertyIsNull()
{
var attribute = new OptionalIfAttribute("OtherProperty");
//**********************
var model = new testModel;//your model that you want to test the validation against
var context = new ValidationContext(testModel, null, null);
var result = attribute.IsValid(testModel, context);
Assert.That(result.Count == 0, Is.True); //is valid or Count > 0 not valid
}
这在没有具体模型的情况下进行测试class:
[TestMethod]
public void When_BothPropertiesAreSet_SuccessResult()
{
var mockModel = new Mock<ISomeModel>();
mockModel.Setup(m => m.SomeProperty).Returns("something");
var attribute = new OptionalIfAttribute("SomeProperty");
var context = new ValidationContext(mockModel.Object, null, null);
var result = attribute.IsValid(string.Empty, context);
Assert.AreEqual(ValidationResult.Success, result);
}
[TestMethod]
public void When_SecondPropertyIsNotSet_ErrorResult()
{
const string ExpectedErrorMessage = "Whoops!";
var mockModel = new Mock<ISomeModel>();
mockModel.Setup(m => m.SomeProperty).Returns((string)null);
var attribute = new OptionalIfAttribute("SomeProperty");
attribute.ErrorMessage = ExpectedErrorMessage;
var context = new ValidationContext(mockModel.Object, null, null);
var result = attribute.IsValid(string.Empty, context);
Assert.AreEqual(ExpectedErrorMessage, result.ErrorMessage);
}
我正在使用下面的代码来测试我的自定义验证器 class
[TestMethod]
public void IsValid_Test()
{
var modelObj = new youModelClass {
requiredProp = value
};
var validatorClassObj = new yourValidatorClass();
var validationResult = validatorClassObj.GetValidationResult( valueToValidate, new ValidationContext( modelObj ) );
Assert.AreEqual( ValidationResult.Success, validationResult );
}
很高兴知道是否还有其他测试方法。