PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Sun Dec 21, 2014 4:30 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Thu May 15, 2008 11:14 pm 
Offline
DevNet Resident
User avatar

Joined: Thu Mar 06, 2008 9:23 am
Posts: 1174
Location: Ann Arbor, MI (USA)
I am trying to get myself into a TDD mindset, (it's hard when I'm just programming on and off when I have free time-- not that often at my school) but I have a question about mocks. Or more specifically, what comes after mocking. I get the idea behind mocking a class-- its a good way to discover the interface for the next class you write. However, what happens next? Do you write the class that was mocked, and gradually replace the mock code with calls to the new class? If so, that seems like the first class would be too dependent on the second class, and kind of goes against TDD methodology. If not, what happens when you change the interface of the new class? Are you just supposed to keep mocks up to date as you go along? That sounds like a nightmare.


Top
 Profile  
 
PostPosted: Sat May 17, 2008 12:13 am 
Offline
Breakbeat Nuttzer
User avatar

Joined: Wed Mar 24, 2004 8:57 am
Posts: 13098
Location: Melbourne, Australia
You use the mock when testing the dependent component. Then you write the class which provides the real implementation with it's own set of tests. If your class provides the implementation that your mock demonstrated then everything should just work.

You can provide a separate set of end-to-end tests using both real components if you want to, but certainly do not edit your first tests to remove the mock.


Top
 Profile  
 
PostPosted: Sat May 17, 2008 9:41 am 
Offline
DevNet Resident
User avatar

Joined: Thu Mar 06, 2008 9:23 am
Posts: 1174
Location: Ann Arbor, MI (USA)
And then what if the interface of the dependent class changes during the course of testing?


Top
 Profile  
 
PostPosted: Sat May 17, 2008 9:29 pm 
Offline
Breakbeat Nuttzer
User avatar

Joined: Wed Mar 24, 2004 8:57 am
Posts: 13098
Location: Melbourne, Australia
allspiritseve wrote:
And then what if the interface of the dependent class changes during the course of testing?


Then yes, you do need to change your mock code. This is a bit of a problem refactoring. Anything which changes an interface will always require code changes both in the production code and in the test code. The trick is to try and minimize the number of places you need to change the code. In your tests use Creation Methods (little factory methods) to set up your object under test and any of it's dependencies (mock or real objects). Mots of the differences will often be in the setup of the object you're testing. If you can delegate that setup to a single method or a few smaller methods then you have a lot less code to change if your interface changes. Anything which minimizes duplication in test code is good in the just the same way avoiding duplication in production code is. You have to maintain your tests just like you have to maintain your production code -- just try to make it as easy as possible to do that.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group