Created
October 16, 2017 12:49
-
-
Save johnmmoss/8aba53a0803c9c3c2f422db062bff9a5 to your computer and use it in GitHub Desktop.
Unit Test the model state in dotnet core
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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