all groups > c# > april 2008 >
You're in the

c#

group:

Writing a DAL with TDD


Writing a DAL with TDD Cramer
4/23/2008 8:16:14 AM
c#: I've finally gotton board with TDD (test driven development) and wow is it
effective! I went from sceptic to True Believer with my first effort.

My question: According to the various books and articles I have read about
TDD, a good unit test does not rely on the database or other such
external/environmental conditions. More generally, a good unit test is
atomic and makes as few assumptions about its runtime environment as
possible. But what about developing unit tests for a DAL - whose purpose in
life is to interact with a database?

Your thoughts and opinions are appreciated.


Re: Writing a DAL with TDD mgsram
4/23/2008 8:44:47 AM
[quoted text, click to view]

I would test the behavior in the DAL. I m not very well versed with DB
programming
but, as far as i know DAL abstracts a bunch of SQL statements or calls
to
stored procedures in the DB.

So I would test for setting up the adapter object that actually
tunnels the call to the DB.
The rationale is that if you have validated the behavior that sets up
the adapter object,
then it will work correctly when you connect a actual DB.

If the Adapter object cannot be mocked direclty, then I would write a
simple wrapper around
Re: Writing a DAL with TDD Mr. Arnold
4/23/2008 10:09:38 PM

[quoted text, click to view]

You could do the unit test of the DAL through the interfaces of the Business
layer that interacts with the DAL.

This link has shows about TDD and unit testing of a TDD solution.

http://www.polymorphicpodcast.com/



There is also this which can show you how to develop your solutions, and how
you can test through the interface. You should be able to develop a test
harness for the interface or use something line Nunit.


MODEL-VIEW-PRESENTER

http://www.polymorphicpodcast.com/

click 'Shows'

click 'Design Patterns Bootcamp: Model View * Patterns*

view parts 1-5
Re: Writing a DAL with TDD John B
4/24/2008 12:00:00 AM
[quoted text, click to view]
Similar to what Ben said,
We used to have a "test" database (or one of each, mssql, mysql, oracle)
that each time would be cleaned out and then repopulated by each test.
It might not be very scaleable but worked well for what we were doing at
the time which encompassed 500+ tests running in around 20 seconds.

Re: Writing a DAL with TDD Ben Voigt [C++ MVP]
4/24/2008 10:52:04 AM
[quoted text, click to view]

The unit test needs to create a new database in a known state, open it using
the DAL under test, verify the results, clean up the database.

[quoted text, click to view]

Re: Writing a DAL with TDD mgsram
4/24/2008 12:00:10 PM
[quoted text, click to view]

Ben,
Then it wont be a unit test in the TDD sense. In fact i would argue
that its not unit test at all and more of Integration testing. The
point is your tests would be "lazy" and would not help you drive the
Re: Writing a DAL with TDD mgsram
4/24/2008 3:43:38 PM
[quoted text, click to view]

Absolutely,
Re: Writing a DAL with TDD Ben Voigt [C++ MVP]
4/24/2008 5:07:00 PM
[quoted text, click to view]

Well... ok. This might no be considered a unit test anymore, it is quite
heavy.

But there's no reason that you couldn't still develop the test before the
code. You could even use the test to drive database layout (this might not
be very effective at normalization though).

Re: Writing a DAL with TDD Cramer
4/24/2008 7:10:44 PM
[quoted text, click to view]


Thank you John,
Along the lines of what you did, I have heard unit tests (against a DB)
doing something like this: (1) open a transaction in the test fixture setup;
(2) run the tests; (3) roll back the transaction in the test fixture
teardown.

-Cramer


AddThis Social Bookmark Button