简体   繁体   中英

Unit Testing the Data Access Layer

I want to test the data layer, how do I go about this? I am using NUnit for my business object testing. Must I use mock objects (which I have never used before)?

I would appreciate any amount of input if someone can just guide me in the right direction.

You would have to do this:

  1. Separate your business layer ( or controller layer) away from your data access. Make sure that your data access layer is fully clean. You need mock to do this.
  2. If you are using an ORM such as NHibernate, change the database vendor to something that's light ( such as sqlite , set it to in-memory in the config) so that you can execute the tests against an in-memory database. In this way, you don't have to worry about messy things such as resource cleanup. Of course, you need to prepopulate your data before doing your tests.

Here's an answer on unit testing DAL that you might find useful.

Using mock objects depends upon your unit tests. If you dont want to initialize many objects for testing a single object you can use a mock to simulate the behavior of the other objects.

For testing DAL objects you will have to reset the DB State every time you run a test. As running tests on your database will create many records which might change the state of the DB.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM