How to write Unit Test for IValidatableObject model

I created a ViewModel as shown below.

   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<ProjectStateViewModel> States { get; set; }
        public string PageTitle { get; set; }


        public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
        {
            List<ValidationResult> res = new List<ValidationResult>();
            if (EndDate <= StartDate)
            {
                res.Add( new ValidationResult(ErrorMessage.END_DATE_AFTER_START_DATE));
            }

            return res;
        }
    }

Also written below is a test case

[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<ProjectStateViewModel> { new ProjectStateViewModel { StateName = "Pending" } }
    };

    Task<ActionResult> task = _projectController.Edit(model);

    ViewResult result = task.Result as ViewResult;

}

It works fine when the actual code is executed in the MVC application through the browser. But ModelState.IsValid returns true in my Unit test, and it fails. Later I realized that the check is not performed by the controller. The controller received the model after validation, so it will have a corresponding ModelState. My question is what should be my approach to writing Unit test to test the model, or should I write at all?

+4
source share
2 answers

, . . , :

[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<ProjectStateViewModel> { new ProjectStateViewModel { StateName = "Pending" } }
    };

    ValidationContext contex = new ValidationContext(model);
    Validator.ValidateObject(model, contex);

}
+3

.

[TestMethod]
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<ProjectStateViewModel> { new ProjectStateViewModel { StateName = "Pending" } }
    };

    var validationContext = new ValidationContext(model);

    var results = model.Validate(validationContext);

    Assert.AreEqual(results.Count(), 1);
    Assert.AreEqual(results.First().ErrorMessage, "End Date must be greater than Start Date.");
}
+9

Source: https://habr.com/ru/post/1547293/


All Articles