Model validation is fundamental to any project: it brings and acting as a first shield against an invalid state. security robustness You should then add Unit Tests focused on model validation. In fact, when defining the input model, you should always consider both the valid and, even more, the invalid models, . making sure that all the invalid models are rejected is a good approach for this scenario, and you can use to implement it gradually. BDD TDD Okay, but how can you validate that the models and model attributes you defined are correct? Let’s define a simple model: public class User { [Required] [MinLength(3)] public string FirstName { get; set; } [Required] [MinLength(3)] public string LastName { get; set; } [Range(18, 100)] public int Age { get; set; } } Have we defined our model correctly? A well-written Unit Test suite is our best friend here! Are we covering all the edge cases? We have two choices: we can write Integration Tests to send requests to our system, which is running an in-memory server, and check the response we receive. Or we can class, the one used by ASP.NET to validate input models, to create slim and fast Unit Tests. Let’s use the second approach. use the internal Validator Here’s a utility method we can use in our tests: public static IList<ValidationResult> ValidateModel(object model) { var results = new List<ValidationResult>(); var validationContext = new ValidationContext(model, null, null); Validator.TryValidateObject(model, validationContext, results, true); if (model is IValidatableObject validatableModel) results.AddRange(validatableModel.Validate(validationContext)); return results; } In short, we create a without any external dependency, focused only on the input model: . validation context new ValidationContext(model, null, null) Next, we by calling and store the validation results in a list, . validate each field TryValidateObject result Finally, if the Model implements the interface, which exposes the method, we call that method and store the returned validation errors in the final list created before. IValidatableObject Validate Validate() result As you can see, we can handle both validations coming from attributes on the fields, such as , and custom validation defined in the model class’s method. [Required] Validate() Now, we can use this method to verify whether the validation passes and, in case it fails, which errors are returned: [Test] public void User_ShouldPassValidation_WhenModelIsValid() { var model = new User { FirstName = "Davide", LastName = "Bellone", Age = 32 }; var validationResult = ModelValidationHelper.ValidateModel(mode); Assert.That(validationResult, Is.Empty); } [Test] public void User_ShouldNotPassValidation_WhenLastNameIsEmpty() { var model = new User { FirstName = "Davide", LastName = null, Age = 32 }; var validationResult = ModelValidationHelper.ValidateModel(mode); Assert.That(validationResult, Is.Not.Empty); } [Test] public void User_ShouldNotPassValidation_WhenAgeIsLessThan18() { var model = new User { FirstName = "Davide", LastName = "Bellone", Age = 10 }; var validationResult = ModelValidationHelper.ValidateModel(mode); Assert.That(validationResult, Is.Not.Empty); } Further readings Model Validation allows you to create robust APIs. To improve robustness, you can follow : more Postel’s law 🔗 Postel’s law for API Robustness | Code4IT This article first appeared on Code4IT 🐧 Model validation, in my opinion, is one of the cases where Unit Tests are way better than Integration Tests. This is a perfect example of , the best (in most cases) way to structure a test suite: Testing Diamond 🔗 Testing Pyramid vs Testing Diamond (and how they affect Code Coverage) | Code4IT If you still prefer writing Integration Tests for this kind of operation, you can rely on the class and use it in your NUnit tests: WebApplicationFactory 🔗 Advanced Integration Tests for .NET 7 API with WebApplicationFactory and NUnit | Code4IT Wrapping up Model validation is crucial. Testing the correctness of model validation can make or break your application. Please don’t skip it! I hope you enjoyed this article! Let’s keep in touch on or ! 🤜🤛 Twitter LinkedIn Happy coding! 🐧 Also published . here