[Article]Simplify Data Layer Unit Testing using Enterprise Services

My first MSDN Magazine article is (finally!) up:
 
"Despite all the hype surrounding unit testing and Test Driven Development (TDD) many developers don't see them as useful processes that are applicable to the production of real-world applications. One reason is that once you get down to the intricacies of application development, all of the easy testing you've heard so much about can vanish. You may find yourself writing unit tests that are hard to implement against real code. This is often the case in database testing, where TDD can quickly lose its appeal, and force you to revert your processes to the previous real-world methodologies your team employed. Here, I'll present some techniques to make your testing process easier, even when developing real-world applications, using unit testing."
 
This should sum up nicely the stuff that I've learned in this arena over the past year or so and should pose the "definitive overview" on this subject for .NET developers. Hopefully.
 
PS
Just in case you're wondering - that one and only "review" down below the article giving the perfect grade to this article - Yeah, that's mine :-)
Published Tuesday, May 10, 2005 10:12 PM by RoyOsherove

Comments

Wednesday, May 11, 2005 4:55 AM by Hugo Rodger-Brown

# re: [Article]Simplify Data Layer Unit Testing using Enterprise Services

It's always good to find out that you're not alone! I posted a while back about the whole unit testing / data access conundrum (http://hugorodgerbrown.blogspot.com/2005/04/unit-testing-and-data-access.html), and it's great to find out that my solution is not complete baloney ;-)
Wednesday, May 11, 2005 8:35 AM by Rik Hemsley

# Great time saver

The 'Services Without Components' trick means I can use transactions for tests. I usually avoid the hassle and just live with a restore before each test, but sometimes this means a suite of tests takes several minutes to run, which is too long when you're anxious to see if you broke anything with your last change.

Nice work!
Wednesday, May 11, 2005 9:00 AM by Rik Hemsley

# One problem

The SWC rollback method doesn't work here. After the teardown code has run, a value inserted into the database is still there. XP SP2...
Wednesday, May 11, 2005 10:51 AM by Roy Osherove

# re: [Article]Simplify Data Layer Unit Testing using Enterprise Services

Rik:
Are you using SP2? or Win2003?
If so, perhaps you have some code under test that creates its own transaction? or is ignoring transactions?

if the answer is still no, can you send me a code sample? to roy at osherove dot com
Thursday, May 12, 2005 12:44 PM by Bob Archer

# re: [Article]Simplify Data Layer Unit Testing using Enterprise Services

Roy,

This is a good article. Would I be able to test my transaction handling code inside the COM+ transaction.

Lets say I want to test that when inserting an Order and related order lines and an order line insert fails that the order insert is rolled back? Will that test work within the COM+ transaction?

Also, it would be great to see you expand the article to include the TransactionScope object in .Net 2.0.

Bob
Saturday, May 14, 2005 5:20 PM by Rik Hemsley

# re: [Article]Simplify Data Layer Unit Testing using Enterprise Services

Rollback does work... but you have to open the connection to the database _after_ opening the new transaction context. Thanks to Roy for setting me straight!
Tuesday, May 17, 2005 3:31 PM by Wayne Allen

# re: [Article]Simplify Data Layer Unit Testing using Enterprise Services

Nice article - best solution I've seen for db testing. However, you missed one important point.

If you use RESTORE how do you ensure your views/stored procedures/stored functions/etc are up to date.

If you don't use RESTORE how do you ensure the initial data state?
Tuesday, May 17, 2005 7:03 PM by Javier Luna

# re: [Article]Simplify Data Layer Unit Testing using Enterprise Services

I believe that any DataLayer must be a simple code block, that they allow operations against DB.

That code block would not have to know on the Business Entities. Single to specialize it is to execute the operations (Store Procedures and SQL Sentences) against the engine DB (SQL, Oracle, DB2, etc.), with which this setting.

Finally, I invite to you to download the DataLayer.Primitives Public Version.

This is very cool Data Layer :)

DataLayer.Primitives - Readme!
http://forums.microsoft.com/msdn/ShowPost.aspx?PostID=1389

Cheers,

Javier Luna
http://guydotnetxmlwebservices.blogspot.com/
Monday, May 23, 2005 3:22 AM by TrackBack

# Roy Osherove's MSDN Article

Roy Osherove blogs about his first MSDN article titled "Simplify Data Layer Unit Testing using...
Tuesday, May 31, 2005 6:07 AM by Tomer Gabel

# re: [Article]Simplify Data Layer Unit Testing using Enterprise Services

Finally found the time to read through the whole thing - it's great in concept, although when I tried to implement a a similar scheme (with XtUnit) a while back I couldn't get it to work (COM+ errors I did not readily know what to do with).

I'll definitely give this a shot next time I write a DAL in .NET (which may be a while yet...)