1

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:

[NotMapped]
public string ValidSignupCode { get { return "VALID_SIGNUP_CODE"; } }

[MaxLength(15)]
[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)

The interesting thing is that this runs fine when running MVC, but not in UnitTest land.

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.

Any advice on ways to get this to work are greatly appreciated.

Daniel
  • 9,491
  • 12
  • 50
  • 66
Martin Noreke
  • 4,066
  • 22
  • 34
  • Sounds like you've got two different versions of Foolproof you're referencing. Go find every copy of the dll and check it. Use fusion logging if you don't find mismatched versions. –  Jun 18 '15 at 15:36
  • I just verified that all my projects (ViewModel, MVC, MVCTest) are referencing the same version of Foolproof. – Martin Noreke Jun 18 '15 at 15:40
  • How? By locating every copy on disk and using a decompiler to examine it? By using fusion logging to see where it is being loaded? This is what you need to do when you've got something freaky going on. Start at the bottom. –  Jun 18 '15 at 15:46
  • I verified the version number via VS. I also deleted all my bin folders so that only one "MVC Foolproof Validation.dll" file exists on disk prior to running the test, and it still fails. Runs fine in MVC. – Martin Noreke Jun 18 '15 at 15:53
  • "Runs fine in MVC" what does that mean? Works at runtime? On the same machine, or on a server somewhere? –  Jun 18 '15 at 15:55
  • Works at run time on multiple machines or in Azure. – Martin Noreke Jun 18 '15 at 15:58
  • So, on an azure website, it runs fine. But it doesn't run locally? Then the version being published to azure is the correct version. You have to have a different version on the machine where it isn't working. You need to use fusion logging to see where the assembly is being loaded from. Go to msdn and find the docs on fusion logging. Configure it, reboot, and run your failing tests again. Go look at the logs for the foolproof dll. –  Jun 18 '15 at 16:01

1 Answers1

1

I ended up downloading the Foolproof code from Codeplex to dig into this one.

public override bool IsValid(object value)
{
    throw new NotImplementedException();
}

The validation method being called isn't implemented. I'll reach out on their message boards to see how to get this resolved instead.

Martin Noreke
  • 4,066
  • 22
  • 34