public static void ValidateObject_throws_ValidationException_if_all_properties_are_valid_but_class_is_invalid() { var objectToBeValidated = new InvalidToBeValidated() { PropertyWithRequiredAttribute = "Valid Value" }; var validationContext = new ValidationContext(objectToBeValidated); var exception = Assert.Throws <ValidationException>( () => Validator.ValidateObject(objectToBeValidated, validationContext, true)); Assert.IsType <ValidClassAttribute>(exception.ValidationAttribute); Assert.Equal( "ValidClassAttribute.IsValid failed for class of type " + typeof(InvalidToBeValidated).FullName, exception.ValidationResult.ErrorMessage); Assert.Equal(objectToBeValidated, exception.Value); }
public static void TryValidateObject_returns_false_if_all_properties_are_valid_but_class_is_invalid() { var objectToBeValidated = new InvalidToBeValidated() { PropertyWithRequiredAttribute = "Valid Value" }; var validationContext = new ValidationContext(objectToBeValidated); Assert.False( Validator.TryValidateObject(objectToBeValidated, validationContext, null, true)); var validationResults = new List <ValidationResult>(); Assert.False( Validator.TryValidateObject(objectToBeValidated, validationContext, validationResults, true)); Assert.Equal(1, validationResults.Count); Assert.Equal("ValidClassAttribute.IsValid failed for class of type " + typeof(InvalidToBeValidated).FullName, validationResults[0].ErrorMessage); }
public static void ValidateObject_throws_ValidationException_if_all_properties_are_valid_but_class_is_invalid() { var objectToBeValidated = new InvalidToBeValidated() { PropertyWithRequiredAttribute = "Valid Value" }; var validationContext = new ValidationContext(objectToBeValidated); var exception = Assert.Throws<ValidationException>( () => Validator.ValidateObject(objectToBeValidated, validationContext, true)); AssertEx.IsType<ValidClassAttribute>(exception.ValidationAttribute); Assert.Equal( "ValidClassAttribute.IsValid failed for class of type " + typeof(InvalidToBeValidated).FullName, exception.ValidationResult.ErrorMessage); Assert.Equal(objectToBeValidated, exception.Value); }
public static void TryValidateObject_returns_false_if_all_properties_are_valid_but_class_is_invalid() { var objectToBeValidated = new InvalidToBeValidated() { PropertyWithRequiredAttribute = "Valid Value" }; var validationContext = new ValidationContext(objectToBeValidated); Assert.False( Validator.TryValidateObject(objectToBeValidated, validationContext, null, true)); var validationResults = new List<ValidationResult>(); Assert.False( Validator.TryValidateObject(objectToBeValidated, validationContext, validationResults, true)); Assert.Equal(1, validationResults.Count); Assert.Equal("ValidClassAttribute.IsValid failed for class of type " + typeof(InvalidToBeValidated).FullName, validationResults[0].ErrorMessage); }