加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 测试ASP.NET MVC 2中的模型绑定

发布时间:2020-12-16 03:18:41 所属栏目:asp.Net 来源:网络整理
导读:第一;我知道我不需要测试MVC的内部,但我真的需要套件测试流入我们系统的数据. 我希望如何在不模仿所有HTTP上下文的情况下,测试objectA(表单集合,字典,集合,对象等)是否符合objectAModel? 我不想实例化我的控制器或调用操作.我只是想测试我的新对象是否使模
第一;我知道我不需要测试MVC的内部,但我真的需要套件测试流入我们系统的数据.

我希望如何在不模仿所有HTTP上下文的情况下,测试objectA(表单集合,字典,集合,对象等)是否符合objectAModel?

我不想实例化我的控制器或调用操作.我只是想测试我的新对象是否使模型状态无效.

我希望我能简单地写一下

var modelState = new ModelBindingContext<objectAModel>().validate(objectA);

解决方法

布拉德威尔逊有一个 excellent post on DataAnnotations

How Do I Test It?

Using the DataAnnotations attributes
for your models moves the validation
out of the controller actions and into
the model binder,which means your
unit tests for your controller actions
will be simplified.

When you’re writing tests for this,
you need to verify three things:

  1. Is the DataAnnotationsModelBinder registered
    as the default binder?
    You’ll only do this once for the whole application,much like the route
    tests you would write.
  2. Is my model properly decorated with DataAnnotations attributes?
    You’ll end up writing tests for each validation attribute that you add
    to your model.
  3. Does my action method properly react when the model state is invalid? You’ll only need to write this once per action method.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读