title: Unit tests on MVC validation
body: <p>How can I test that my controller action is putting the correct errors in the ModelState when validating an entity, when I'm using DataAnnotation validation in MVC 2 Preview 1?</p>
  
  <p>Some code to illustrate.  First, the action:</p>
  
  <pre><code>    [HttpPost]
      public ActionResult Index(BlogPost b)
      {
          if(ModelState.IsValid)
          {
              _blogService.Insert(b);
              return(View("Success", b));
          }
          return View(b);
      }
  </code></pre>
  
  <p>And here's a failing unit test that I think should be passing but isn't (using MbUnit &amp; Moq):</p>
  
  <pre><code>[Test]
  public void When_processing_invalid_post_HomeControllerModelState_should_have_at_least_one_error()
  {
      // arrange
      var mockRepository = new Mock&lt;IBlogPostSVC&gt;();
      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);
  }
  </code></pre>
  
  <p>I guess in addition to this question, <em>should</em> I be testing validation, and should I be testing it in this way?</p>