Mock the DAL or Mock the Db

by alski 4. February 2008 21:24

Now they say that the only thing your can be sure of as an aspiring good developer is that the "good" code you write today will become the "bad" code of tomorrow. So with that in mind, its time for a turnaround of opinion that so big I should be applying for a government position.

Old Opinion

Unit testing and databases doesn't work. Instead you should mock your DAL.

New opinion

It may be better to use a database for unit testing when you have an easily scriptable database build.

Why?

I've recently put a lot of work into developing a mockable DAL.

  • I refactored all my data access into a single class
  • I made the class virtual
  • I went through each test scenario and recreated a series of calls to supply the data
    • This step took considerable effort in first ascertaining the order of the calls and then supplying the correct into. These are not trivial cases.
    • This code is disposable. It is never used in a final product. It is only for Unit testing.
  • I added Asserts to ensure my results were correct

Instead of doing all the work to Mock the DAL what I should do is Mock the Db. If I had a simple way of creating a Unit test database then I wouldn't need to write all the disposable code.

I've also been having problems keeping my pre-prod database and my development database in sync, so articles like Jeff's Get Your Database Under Version Control or Is your database under version control make sense.

Although its only today that the unit testing light-bulb has clicked on.

What I really need is a Mock Db. A process scripts it into life. Unit tests can truncate tables and insert just the rows that they need.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , ,

Code | SQL

Powered by BlogEngine.NET 1.4.5.0
Theme by Mads Kristensen

RecentComments

Comment RSS