Unit tests on MVC validation

前端 未结 12 1213
离开以前
离开以前 2020-12-04 06:31

How can I test that my controller action is putting the correct errors in the ModelState when validating an entity, when I\'m using DataAnnotation validation in MVC 2 Previe

12条回答
  •  温柔的废话
    2020-12-04 07:05

    If you care about validation but you don't care about how it is implemented, if you only care about validation of your action method at the highest level of abstraction, no matter whether it is implemented as using DataAnnotations, ModelBinders or even ActionFilterAttributes, then you could use Xania.AspNet.Simulator nuget package as follows:

    install-package Xania.AspNet.Simulator
    

    --

    var action = new BlogController()
        .Action(c => c.Index(new BlogPost()), "POST");
    var modelState = action.ValidateRequest();
    
    modelState.IsValid.Should().BeFalse();
    

提交回复
热议问题