What would you put into the unit test of a repository class (data access layer)?

别等时光非礼了梦想. 提交于 2019-12-05 01:30:18

问题


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!


回答1:


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:

  • saving new object
  • change -> persist -> restore sequence
  • all 'Find' methods

Essentially you testing the correctness of:

  • mappings (even if you use fluent)
  • criteria
  • hql or sql queries

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.




回答2:


  1. Need to test proper Exception handling
  2. Time Out Parameter of Database Connection
  3. Time Out Parameter of Store Procedure invocation
  4. Proper input parameters mapping . If store procedure expects to receive float but receive int.



回答3:


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 ;)



来源:https://stackoverflow.com/questions/7461326/what-would-you-put-into-the-unit-test-of-a-repository-class-data-access-layer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!