你会放入仓储类(数据访问层)的单元测试?你会、放入、单元测试、数据

2023-09-03 15:05:52 作者:威龙丶断魂神狙

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

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)几乎是不可能的。请看看这个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查询

交易通常是通过应用层,不储存库处理。您可能会感兴趣的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.