What would you put into the unit test of a repository class (data access layer)?
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!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.
- Need to test proper Exception handling
- Time Out Parameter of Database Connection
- Time Out Parameter of Store Procedure invocation
- 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 ;)
精彩评论