在 ASP.NET MVC 2 中测试模型绑定

发布于 2024-08-13 00:09:19 字数 309 浏览 4 评论 0原文

第一的;我知道我不需要测试 MVC 的内部结构,但我确实需要一套围绕流入我们系统的数据进行测试的套件。

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

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

我希望我能简单地写

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

First; I know that I should not need to test the internals of MVC but I REALLY need a suite a tests around data flowing into our system.

How can I, I hope without mocking all of HTTP context, test that objectA (form collection, dict, collection, object, etc) does or does not conform to objectAModel?

I'd like to not have to instantiate my controller or call the action. I simply want to test if my new object invalidates the modelstate.

I wish I could simply write

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

阳光下的泡沫是彩色的 2024-08-20 00:09:19

Brad Wilson 有一篇关于 DataAnnotations 的优秀帖子


如何测试它?

使用 DataAnnotations 属性
为您的模型移动验证
脱离控制器动作并进入
模型活页夹,这意味着您的
控制器操作的单元测试
将被简化。

当你为此编写测试时,
您需要验证三件事:

  1. DataAnnotationsModelBinder 是否已注册
    作为默认活页夹?
    对于整个应用程序,您只需执行一次此操作,就像路线一样
    你会写的测试。
  2. 我的模型是否用 DataAnnotations 属性正确修饰?
    您最终将为添加的每个验证属性编写测试
    到你的模型。
  3. 当模型状态无效时,我的操作方法是否能正确反应?
    您只需为每个操作方法编写一次。

Brad Wilson has an 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.
因为看清所以看轻 2024-08-20 00:09:19

非常简单,您可以实现一个方法来提取模型类型的 ModelMetadata,获取 ModelValidator,并验证模型对象。

public bool IsModelValid<T>(T model) where T : class
{
    var metaData = ModelMetadataProviders.Current.GetMetadataForType(() => model, typeof(T));
    var validator = ModelValidator.GetModelValidator(metaData, new ControllerContext());
    var validationResults = validator.Validate(model);
    return 0 == validationResults.Count();
}

我所说的“简单”是指这不一定要考虑所有可能的配置,但您可以对模型的有效性进行基本检查。

Very simply, you can implement a method that pulls the ModelMetadata for your model's type, gets the ModelValidator, and validates the model object.

public bool IsModelValid<T>(T model) where T : class
{
    var metaData = ModelMetadataProviders.Current.GetMetadataForType(() => model, typeof(T));
    var validator = ModelValidator.GetModelValidator(metaData, new ControllerContext());
    var validationResults = validator.Validate(model);
    return 0 == validationResults.Count();
}

By "simply", I mean this does not necessarily take all possible configurations in to consideration, but you can get a basic check on your model's validity.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文