/// <summary> /// Manually sets Model State /// Calling controller directly bypasses model binder /// Without calling this method, ModelState.IsValid will pass for invalid models /// </summary> /// <param name="controller">GeocachesController to change model state</param> /// <param name="model">Geocache object to validate</param> protected void ValidateModel(ref GeocachesController controller, object model) { controller.ModelState.Clear(); List <ValidationResult> validationResults = new List <ValidationResult>(); Validator.TryValidateObject(model, new ValidationContext(model, null, null), validationResults, true); foreach (ValidationResult result in validationResults) { foreach (string name in result.MemberNames) { // No model binder when calling controller directly, must manually set invalid Model State controller.ModelState.AddModelError(name, result.ErrorMessage); } } }
public virtual void Arrange() { geocacheRepository = new Mock <IGeocacheRepository>(); controller = new GeocachesController(geocacheRepository.Object); }