How can I test Controller.ViewData.ModelState
? I would prefer to do it without any mock framework.
Below the Form, the ModelState. IsValid property is checked and if the Model is valid, then the value if the ViewBag object is displayed using Razor syntax in ASP.Net MVC.
The ModelState has two purposes: to store the value submitted to the server, and to store the validation errors associated with those values.
ModelState. IsValid indicates if it was possible to bind the incoming values from the request to the model correctly and whether any explicitly specified validation rules were broken during the model binding process. In your example, the model that is being bound is of class type Encaissement .
When unit testing controller logic, only the contents of a single action are tested, not the behavior of its dependencies or of the framework itself. Set up unit tests of controller actions to focus on the controller's behavior. A controller unit test avoids scenarios such as filters, routing, and model binding.
You don't have to use a Mock if you're using the Repository Pattern for your data, of course.
Some examples: http://www.singingeels.com/Articles/Test_Driven_Development_with_ASPNET_MVC.aspx
// Test for required "FirstName". controller.ViewData.ModelState.Clear(); newCustomer = new Customer { FirstName = "", LastName = "Smith", Zip = "34275", }; controller.Create(newCustomer); // Make sure that our validation found the error! Assert.IsTrue(controller.ViewData.ModelState.Count == 1, "FirstName must be required.");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With