Skip to content

Instantly share code, notes, and snippets.

@johnmmoss
Created October 16, 2017 12:49
Show Gist options
  • Save johnmmoss/8aba53a0803c9c3c2f422db062bff9a5 to your computer and use it in GitHub Desktop.
Save johnmmoss/8aba53a0803c9c3c2f422db062bff9a5 to your computer and use it in GitHub Desktop.
Unit Test the model state in dotnet core
var controller = new CustomerController(_customerService.Object);
var newModel = new CreateModel();
var validationContext = new ValidationContext(newModel, null, null);
var validationResults = new List<ValidationResult>();
Validator.TryValidateObject(newModel, validationContext, validationResults, true);
foreach (var validationResult in validationResults)
{
controller.ModelState.AddModelError(validationResult.MemberNames.FirstOrDefault() ?? string.Empty, validationResult.ErrorMessage);
}
Assert.AreEqual(4, controller.ModelState.ErrorCount);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment