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.


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.

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

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.

Need Your Help

Powershell get-eventlog message column is too short

powershell event-log

When using powershell to retrieve info about events Message column gets trimmed and is too short:

Low throughput while using http remoting C#

c# performance proxy remoting

I am using http remoting in C# to talk between two applications.