开发者

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

开发者 https://www.devze.com 2023-04-05 11:32 出处:网络
I\'d like to write a unit test for my data access layer to make sure that everything works allright in it.

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
开发者_如何学Python

Is there anything else about a DAL that is worth testing?

Thanks in advance for your answers!


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.


  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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消