mardi 1 mars 2016

Unit Testing Remote Validation

I am trying to test my remote validator but I am unable to get the value from Validator.TryValidateObject(model, validationContext, results, true). I can get almost all the other validators but not the remote one

ViewModel

    [Remote("ExistingUser", "Admin", ErrorMessage = "An account with this UserName already exists. Please enter a unique UserName")]
    public string UserName { get; set; }

Controller

    public JsonResult ExistingUser(string userName)
    {
        return Json(UserManager.FindByName(userName) == null, JsonRequestBehavior.AllowGet);
    }

I am getting the validation results using this helper

    public static IList<ValidationResult> Validate(object model)
    {
        var results = new List<ValidationResult>();
        var validationContext = new ValidationContext(model, null, null);
        Validator.TryValidateObject(model, validationContext, results, true);
        if (model is IValidatableObject) (model as IValidatableObject).Validate(validationContext);
        return results;
    }

Aucun commentaire:

Enregistrer un commentaire