---
title: "How to do Unit tests on MVC validation"  
description: "How to do Unit tests on MVC validation"  
author: "Anonymous User"  
published: 2014-11-21  
updated: 2014-11-22  
canonical: https://www.mindstick.com/forum/12675/how-to-do-unit-tests-on-mvc-validation  
category: "asp.net mvc"  
tags: ["mvc4", "unit testing", "validation"]  
reading_time: 2 minutes  

---

# How to do Unit tests on MVC validation

How can I test that my [controller action](https://www.mindstick.com/forum/34400/how-to-retrieve-form-values-in-controller-action) is putting the correct errors in the ModelState when validating an [entity](https://www.mindstick.com/forum/160562/database-connectivity-using-entity-framework-database-first-approach), when I'm using DataAnnotation [validation in MVC](https://www.mindstick.com/forum/34733/how-can-use-validation-in-mvc) 2 [Preview](https://www.mindstick.com/forum/429/i-want-to-get-full-path-of-lt-asp-fileupload-gt-and-then-client-can-click-the-preview-button-to-see-what-file-he-here-has-uploaded) 1?

Some code to illustrate. First, the action:

```
    [HttpPost]    public ActionResult Index(BlogPost b)    {        if(ModelState.IsValid)        {           
          _blogService.Insert(b);            return(View("Success",b));        }        return View(b);    }
```

And here's a failing [unit test](https://www.mindstick.com/forum/157908/what-is-a-unit-test-how-can-unit-tests-use-to-ensure-the-correctness-and-robustness-of-software) that I think should be passing but isn't (using MbUnit & Moq):

```
[Test]public void When_processing_invalid_post_HomeControllerModelState_should_have_at_least_one_error(){    // arrange    var mockRepository = new Mock<IBlogPostSVC>();    var homeController = new HomeController(mockRepository.Object);     // act    var p = new BlogPost { Title = "test" };  // date and content should be required    homeController.Index(p);     // assert Assert.IsTrue(!homeController.ModelState.IsValid);}
```

I guess in addition to this [question](https://www.mindstick.com/blog/23175/how-to-solve-neet-question-paper-in-less-time), should I be [testing](https://www.mindstick.com/articles/1849/role-of-testing-in-software-development) validation, and should I be testing it in this way?

## Replies

### Reply by Anonymous User

Instead of passing in a BlogPost you can also declare the actions parameter as FormCollection. Then you can create the BlogPost yourself and call UpdateModel(model, formCollection.ToValueProvider());. This will trigger the [validation](https://www.mindstick.com/articles/12234/validation-using-data-annotation-using-entity-framework) for any field in the FormCollection.

```
    [HttpPost]    public ActionResult Index(FormCollection form)    {        var b = new BlogPost();        TryUpdateModel(model, form.ToValueProvider());         if (ModelState.IsValid)        {            _blogService.Insert(b);            return (View("Success", b));        }        return View(b);    }
```

Just make sure your test adds a null value for every field in the views form that you want to leave empty.

I found that doing it this way, at the expense of a few extra lines of code, makes my unit tests resemble the way the code gets called at runtime more closely making them more valuable. Also you can test what happens when someone enters "abc" in a control bound to an int property.


---

Original Source: https://www.mindstick.com/forum/12675/how-to-do-unit-tests-on-mvc-validation

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
