How can I test ModelState?

前端 未结 6 1408
轮回少年
轮回少年 2020-12-04 21:42

How can I test Controller.ViewData.ModelState? I would prefer to do it without any mock framework.

6条回答
  •  Happy的楠姐
    2020-12-04 22:06

    For testing Web API, use the Validate method on the controller:

    var controller = new MyController();
    controller.Configuration = new HttpConfiguration();
    var model = new MyModel();
    
    controller.Validate(model);
    var result = controller.MyMethod(model);
    

提交回复
热议问题