Testing ModelState is always valid in asp.net mvc

后端 未结 7 1132
野趣味
野趣味 2020-12-09 16:29

When testing my controller\'s actions the ModelState is always valid.

public class Product
{
    public int Id { get; set; }

    [Required]
    [StringLengt         


        
7条回答
  •  感动是毒
    2020-12-09 16:48

    1. Create Instance of your controller class.
    2. Add model state and call After adding model state
    3. the modelState always give false

      controller.ModelState.AddModelError("key", "error message");
      
      var invalidStateResult = _controller.Index();
      
      Assert.IsNotNull(invalidStateResult);
      

提交回复
热议问题