In my unit tests, I would like to force the validation of a code first POCO that has DataAnnotations on it.
The MVC framework must be doing it behind the scenes, and basically I would like to know how, so I can hopefully make use of it.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It’s the default model binder that is responsible for invoking the validation of the model once it has bound it from the request values.
You could invoke the validation process manually by using a
ValidationContext.Let’s suppose that you have a model:
and then you could unit test it:
Alternatively to using DataAnnotations you could use FluentValidation.NET to perform validation on your view models. It integrates nicely with ASP.NET MVC and it allows you to unit test your validators in a very elegant way.