你会放入仓储类(数据访问层)的单元测试? [英] What would you put into the unit test of a repository class (data access layer)?

查看:195
本文介绍了你会放入仓储类(数据访问层)的单元测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个单元测试我的数据访问层,以确保一切工作还好吧在里面。 现在的问题是,我应该把什么样的东西进入测试?

I'd like to write a unit test for my data access layer to make sure that everything works allright in it. The question is, what kind of things should I put into the tests?

该DAL是一个静态类,它隐藏了底层(流利NHibernate的),并通过的IQueryable

The DAL is a static Repository class which hides the underlying layer (Fluent NHibernate) and exposes stuff to the public through an IQueryable.

我想到了

  • 在CRUD(创建/检索/更新/删除)操作
  • 交易

还有什么关于DAL是值得测试?
预先感谢您的回答!

Is there anything else about a DAL that is worth testing?
Thanks in advance for your answers!

推荐答案

仓库实现与集成测试,而不是单元测试测试。隔离仓库实现(嘲讽ORM)几乎是不可能的。请看看这个<一href="http://stackoverflow.com/questions/7110981/the-repository-itself-is-not-usually-tested/7111748#7111748">answer.集成测试使用一个真正的ORM结合真的假的(通常是在内存中),数据库执行以下操作:

Repository implementation is tested with integration tests, not unit tests. Isolating repository implementation (mocking ORM) is almost impossible. Please take a look at this answer. Integration test uses a real ORM combined with real or fake (usually in-memory) database to do following:

  • 在保存新的对象
  • 更改 - >坚持 - >还原序列
  • 在所有的查找方法

基本上你测试的正确性:

Essentially you testing the correctness of:

  • 在映射(即使你用流利)
  • 标准
  • 在HQL或SQL查询

交易通常是通过应用层,不储存库处理。您可能会感兴趣的<一个href="http://stackoverflow.com/questions/7322434/iqueryable-repositories-take-2/7323598#7323598">this回答。在仓库实现封装的IQueryable将使测试更容易给你。

Transactions are usually handled by an application layer, not repositories. You might be interested in this answer. Encapsulating IQueryable in the repository implementation will make testing a lot easier for you.

这篇关于你会放入仓储类(数据访问层)的单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆