How to correctly unit test my DAL?

一笑奈何 提交于 2019-11-29 07:33:49

问题


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.


回答1:


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.




回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/309708/how-to-correctly-unit-test-my-dal

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