1

I am creating controller unit tests and am trying to programmatically validate a view model with a remote validation. Built in and validationattributes all fire and return correctly, but the remote validation will not execute.

All the validations work in the form post. Is there something else the post is doing that I need to do in the unit test.

Here is the code I am using to validate the view model:

    public static void ValidateViewModel<TViewModel, TController>(this TController controller, TViewModel viewModelToValidate)
          where TController : Controller
    {
        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);
        }
    }

Thanks

Rob
  • 14,746
  • 28
  • 47
  • 65
user1558501
  • 61
  • 1
  • 4

0 Answers0