How to mock ModelState.IsValid using the Moq framework?

后端 未结 3 1404
傲寒
傲寒 2020-12-24 04:19

I\'m checking ModelState.IsValid in my controller action method that creates an Employee like this:

[HttpPost]
public virtual ActionResult Crea         


        
3条回答
  •  忘掉有多难
    2020-12-24 05:00

    You don't need to mock it. If you already have a controller you can add a model state error when initializing your test:

    // arrange
    _controllerUnderTest.ModelState.AddModelError("key", "error message");
    
    // act
    // Now call the controller action and it will 
    // enter the (!ModelState.IsValid) condition
    var actual = _controllerUnderTest.Index();
    

提交回复
热议问题