Model Validation In ASP.NET MVC Test Framework

Idea behind Model Binder

As defined in MSDN: A model binder in MVC provides a simple way to map posted form values to a .NET Framework type and pass the type to an action method as a parameter. Binders also give you control over the deserialization of types that are passed to action methods. Model binders are like type converters, because they can convert HTTP requests into objects that are passed to an action method. However, they also have information about the current controller context.
 
A model binder lets you associate a class that implements the IModelBinder interface using an action-method parameter or using a type. The IModelBinder interface contains a GetValue method that the framework calls in order to retrieve the value of a specified parameter or type. The DefaultModelBinder class works with most .NET Framework types, including arrays and IList, ICollection, and IDictionary objects.
 
If 'ModelState.IsValid' is false means your post data are not following the patterns which is defined in model class properties and add error message into 'ModelState.AddModelError' dictionary object and populate errors in respective view.
 
Above mechanism will happen automatically during runtime using default model binder when controller is called from HTTP request .
 
Model Binder in Test Framework

Model Binder runs automatically during runtime in ASP.NET MVC framework but not in Test Framework then how we can do model validation in Test Framework. Let us create both project in one solution as below:
 
 
 Below is the model class decorated with email validation.
 
 
 I have created Action method as 'Post' and passing 'Name' and 'Email' properties to 'Register' Model class.
 
 
Index View with 'Register View' as Action Link as below:
 
After clicking on Action link and post model properties as below  
 


'IsValid' boolean property is true because i am passing email id in correct format as below:
 
 
Here I am passing email id as wrong format so 'IsValid' property is false as below:
 
 
 
 
 
Let us do the above steps in Test project and run our test cases. I am passing email in wrong format but 'IsValid' property is true. Please check below code:
 
 
 
That means our model validation is not triggered from test project then how we can validate? Let us write some code for model validation, I am providing extension method to Controller class. Below code will provide the model validation, this code will inject automatically using MVC framework with HTTP request so no need to write below code when you are calling controller from HTTP Request.

 
 
Added 'ValidationModel' Extension method to controller class and passing model object as below: 
 
 
 
Now Error fired because i am passing wrong email id so model validation happened in test framework and you can see that 'IsValid' is false .
 
 
 
 
 
 
 
Here I am passing correct email id so no error fired that is why count is zero and 'IsValid' is true.
 
 

 


Using above procedure we can validate model in test framework also.


Similar Articles