March 2012 Entries

UnitTesting Controller DataAnnotation Validation in MVC3 : ModelState.IsValid always true ?

Testing for ModelState.IsValid when unit testing your controller is not straightforward, since the Model Binding is responsible for validating you model. But Model Binding happens in OnActionExecuted filter (and NOT when you call your controller Action method). That means in your test context, Model Binding will not happen when you call your action method and ModelState.IsValid will always be true. But thanksfully, there is a solution below If we have the following simplistic model : public class ......