Ray
Ray

Reputation: 12441

How to correctly unit test my DAL?

I'm new to unit testing. But how do I unit test my DAL which is written with Entity Framework, so I can make sure my DAL code is working correctly but no database is actually touched? Could someone give as much detail as possible please.

Upvotes: 13

Views: 5177

Answers (3)

Randolpho
Randolpho

Reputation: 56391

Unit testing a DAL is a very common headache in development. For the most part, I suggest you skip it.

Most ORMs these days offer some sort of query language, be it LINQ or HQL, or some other flavor. Because a proper unit test requires that you not actually hit the database, you have to mock the ORM and doing that is the biggest pain in the ass you can think of. It's not worth it, IMO. Ultimately, you only end up testing that you wrote the proper query in your code; you get no regression value at all and can better serve your purposes by inspection of the code.

I'm not saying you shouldn't test your use of the DAL, however; just don't try unit testing. You should still have a suite of integration and user acceptance tests for your program/system; let those handle testing your data access instead.

Upvotes: 5

Ricardo Villamil
Ricardo Villamil

Reputation: 5107

When I unit test my DAL I use transactions and rollback at the end of the unit test, so the db is clean.

Upvotes: 4

Garry Shutler
Garry Shutler

Reputation: 32698

If you want to test that your data access layer works correct you really need to test it against a database at some point as otherwise you aren't actually testing it works.

Upvotes: 17

Related Questions