How to TDD a database version of Entity Gateway

137 views
Skip to first unread message

Ken Chien

unread,
Jan 28, 2014, 4:37:49 PM1/28/14
to clean-code...@googlegroups.com
Hi everyone,

I just discovered this mailing list a few weeks ago although I have purchased several of the Clean Code screencasts.
Lots of good discussion here.

My question is: When you are test-driving your implementation of a database backed Entity Gateway, specifically a read/select method which finds an entity from the database, how would you seed the database?

Would you create an entity, persist it, then retrieve it back using the gateway?
OR:
What if your framework (Rails, in this case) provides an easy way to create entries in the database via framework classes that you subclass (i.e. ActiveRecord)? Would you use this shortcut?

After writing this question, I feel that most people would lean towards the former.  It demonstrates how a client -- an interactor -- might use the gateway interface.  I see one problem with this; it's that I would have to implement saving before implementing a read/select/find operation.  

Thoughts?

Roberto Guerra

unread,
Jan 29, 2014, 7:13:04 PM1/29/14
to clean-code...@googlegroups.com
Why would you need to do it in that order?

I would use mocks in this case, just to make sure that the proper methods calls are being made on the ORM I'm using. No need to test that the record is being saved or extracted, that is the ORM's responsibility (and hopefully, you are not maintaining it).
I would hit the database in an integration test though.

Andreas Grimm

unread,
Jan 30, 2014, 1:25:53 AM1/30/14
to clean-code...@googlegroups.com
Ken, you should watch this: http://pluralsight.com/training/courses/TableOfContents?courseName=outside-in-tdd&highlight=mark-seemann_outside-in-tdd-m1-walking-skeleton*17,15#outside-in-tdd-m1-walking-skeleton

While creating his walking skeleton Mark gives a detailed example on how to create a database and insert to it in a completely automated fashion

daniphp

unread,
Feb 1, 2014, 5:37:36 PM2/1/14
to clean-code...@googlegroups.com
Ken, 
if you are thinking of an "unit test" you should use mocking,
if you are thinking of "integration tests" you could use a temporary database (I personally use an xml file to create the initial seeding)

Cheers
Reply all
Reply to author
Forward
0 new messages