如何将 LinqToSql 与元数据注释集成

发布于 2024-07-19 18:45:50 字数 268 浏览 8 评论 0原文

我刚刚开始一个新的 MVC 项目,并且像一个好孩子一样,我试图尽可能长时间地推迟使用数据库。 这是独家新闻:

  1. 我计划使用 ComponentModel.DataAnnotations 装饰。
  2. 我还计划使用 LinqToSql

是否可以针对 DataAnnotations 元数据类编写单元测试? 我不想尽可能晚地放置这样的模式,但我仍然想编写测试来验证模型。

关于好的方法有什么想法吗? 也许是完全不同的东西?

I'm just getting started on a new MVC project, and like a good boy I am trying to defer going to the DB for as long as possible. Here's the scoop:

  1. I'm planning on using the ComponentModel.DataAnnotations decorations.
  2. I'm also planning on using LinqToSql

Is is possible to write a unit test against the DataAnnotations metadata classes? I don't want to put a schema such until as late as possible but I'd still like to write tests to validate the model.

Any ideas on a good approach? Maybe something completely different?

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

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

发布评论

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

评论(1

红墙和绿瓦 2024-07-26 18:45:50

Brad Wilson 写了一篇关于使用 DataAnnotations 及其单元测试的精彩博客文章; http://bradwilson.typepad.com/blog/2009/04/index.html测试

策略的要点是:

  1. 编写一个测试来检查以确保您正在使用 DataAnnotationsModelBinder 作为默认绑定器。
  2. 使用反射来确保您用来装饰模型属性的 DataAnnotation 属性符合您的预期。
  3. 测试“编辑”或“创建”控制器操作时,人为注入 ModelErrors 以测试您是否按预期处理它们。

基本原理是,您将 System.ComponentModel.DataAnnotations 视为基础设施的一部分,并仅测试您如何使用它。

Brad Wilson wrote a great blog post about using DataAnnotations and unit testing them; http://bradwilson.typepad.com/blog/2009/04/index.html

The gist of the testing strategy is this:

  1. Write a test to check to make sure you are using the DataAnnotationsModelBinder as the default binder.
  2. Use reflection to make sure the DataAnnotation attributes you have decorated the model properties with are what you expect.
  3. When testing the Edit or Create controller actions, artificially inject ModelErrors to test that you are handling them as expected.

The rationale is that you treat System.ComponentModel.DataAnnotations like a piece of infrastructure and just test how you have utilized it.

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