Issues when unit testing MVC Validation for consuming project


I am doing unit testing of my validation logic in MVC using the following helper method:
public static void ValidateViewModel(Controller controller, object viewModelToValidate)
        var validationContext = new ValidationContext(viewModelToValidate, null, null);
        var validationResults = new List<ValidationResult>();
        Validator.TryValidateObject(viewModelToValidate, validationContext, validationResults, true);
        foreach (var validationResult in validationResults)
            controller.ModelState.AddModelError(validationResult.MemberNames.FirstOrDefault() ?? string.Empty, validationResult.ErrorMessage);
One of my data models contains the following:
    public string ValidSignupCode { get { return "VALID_SIGNUP_CODE"; } }
    [EqualTo("ValidSignupCode", ErrorMessage = "Sign up Code is not valid.")]
    public string SignupCode { get; set; }
This throws a NotImplementedException from Foolproof with this stacktrace:
       at Foolproof.ModelAwareValidationAttribute.IsValid(Object value)
       at System.ComponentModel.DataAnnotations.ValidationAttribute.IsValid(Object value, ValidationContext validationContext)
       at System.ComponentModel.DataAnnotations.ValidationAttribute.GetValidationResult(Object value, ValidationContext validationContext)
       at System.ComponentModel.DataAnnotations.Validator.TryValidate(Object value, ValidationContext validationContext, ValidationAttribute attribute, ValidationError& validationError)
       at System.ComponentModel.DataAnnotations.Validator.GetValidationErrors(Object value, ValidationContext validationContext, IEnumerable`1 attributes, Boolean breakOnFirstError)
       at System.ComponentModel.DataAnnotations.Validator.GetObjectPropertyValidationErrors(Object instance, ValidationContext validationContext, Boolean validateAllProperties, Boolean breakOnFirstError)
       at System.ComponentModel.DataAnnotations.Validator.GetObjectValidationErrors(Object instance, ValidationContext validationContext, Boolean validateAllProperties, Boolean breakOnFirstError)
       at System.ComponentModel.DataAnnotations.Validator.TryValidateObject(Object instance, ValidationContext validationContext, ICollection`1 validationResults, Boolean validateAllProperties)
I dug into the source code for Foolproof and found the following:
    public abstract class ModelAwareValidationAttribute : ValidationAttribute
        public override bool IsValid(object value)
            throw new NotImplementedException();
The interesting thing is that this runs fine when running MVC, but not in UnitTest land. It appears that different methods are being called for validation when called through MVC framework and the Validator method I am trying to use.

My goal is to unit test validations and bindings through the MVC interface in order to ensure changes don't have far reaching effects as the project gets bigger.

I don't know if this issue is on my side or Foolproof, so any advice on ways to get this to work are greatly appreciated.
Closed Feb 20, 2016 at 9:29 PM by Dromar
fixed in the current version (serversided validation added)


Dromar wrote Feb 20, 2016 at 9:28 PM

Yes, this was a shortcoming and will is adressed in the new version

wrote Feb 20, 2016 at 9:29 PM