title: How to write Unit Test for IValidatableObject Model
body: <p>I have created a ViewModel as below.</p>
  
  <pre><code>   public class ProjectViewModel: IValidatableObject
      {
          public int ProjectID { get; set; }
  
          [DisplayName("Name")]
          [Required]
          public string Name { get; set; }
  
          [DisplayName("Start Date")]
          public DateTime StartDate { get; set; }
  
          [DisplayName("End Date")]
          [Compare("EndDate")]
          public DateTime EndDate { get; set; }
  
          [DisplayName("Project States")]
          public List&lt;ProjectStateViewModel&gt; States { get; set; }
          public string PageTitle { get; set; }
  
  
          public IEnumerable&lt;ValidationResult&gt; Validate(ValidationContext validationContext)
          {
              List&lt;ValidationResult&gt; res = new List&lt;ValidationResult&gt;();
              if (EndDate &lt;= StartDate)
              {
                  res.Add( new ValidationResult(ErrorMessage.END_DATE_AFTER_START_DATE));
              }
  
              return res;
          }
      }
  </code></pre>
  
  <p>Also written the below test case</p>
  
  <pre><code>[TestMethod]
  [ExpectedException( typeof(ValidationException),"End Date must be greater than Start Date.")]
  public void Create_New_Project_EndDate_Earlier_StartDate()
  {
      ProjectViewModel model = new ProjectViewModel
      {
          ProjectID = 3,
          Name = "Test Project",
          StartDate = DateTime.Now.Date,
          EndDate = DateTime.Now.AddMonths(-1),
          States = new List&lt;ProjectStateViewModel&gt; { new ProjectStateViewModel { StateName = "Pending" } }
      };
  
      Task&lt;ActionResult&gt; task = _projectController.Edit(model);
  
      ViewResult result = task.Result as ViewResult;
  
  }
  </code></pre>
  
  <p>it works fine when actual code executing in MVC app through browser.  But ModelState.IsValid returns always true in my Unit test and it fails.  <strong>Later I understood that Validation is not executed by controller.</strong>  Controller received the model after Validation happens so it'll have appropriate ModelState.  <strong>My question is what should be my approach to write unit test for Model Validation or should I write it at all?</strong></p>