您会将什么放入存储库类(数据访问层)的单元测试中?
我想为我的数据访问层编写一个单元测试,以确保其中的一切正常工作。 问题是,我应该把什么样的东西放入测试中?
DAL 是一个静态Repository
类,它隐藏底层(Fluent NHibernate)并通过IQueryable
向公众公开内容。
我考虑过
- CRUD(创建/检索/更新/删除)操作
- 事务
关于 DAL 还有什么值得测试的吗?
预先感谢您的回答!
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?
The DAL is a static Repository
class which hides the underlying layer (Fluent NHibernate) and exposes stuff to the public through an IQueryable
.
I thought about
- CRUD (Create/Retrieve/Update/Delete) operations
- Transactions
Is there anything else about a DAL that is worth testing?
Thanks in advance for your answers!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
存储库实现是通过集成测试而不是单元测试来测试的。隔离存储库实现(模拟 ORM)几乎是不可能的。请查看这个答案。集成测试使用真实的 ORM 与真实或虚假(通常在内存中)数据库相结合来执行以下操作:
本质上,您测试以下内容的正确性:
事务通常由应用程序层处理,而不是存储库。您可能对这个答案感兴趣。在存储库实现中封装 IQueryable 将使您的测试变得更加容易。
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:
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.
接收整数。
receive int.
通常在 DAL 中,您没有业务逻辑,只有简单的数据库访问代码,可能有 1-5 行长,所以没有太多需要测试...
如果您确定要对其进行单元测试,那么我相信增删改查没问题。模拟 NHibernate,提供虚假数据并针对该虚假数据进行测试;)。
希望这有帮助;)
Usually in a DAL you don't have business logic, just plain db access code which is probably 1-5 lines long, so there is not to much to test ...
If you are sure you want to unit test it then i believe CRUD is fine. Mock out NHibernate, provide fake data and test against that fake data ;).
Hope this helps ;)