DEV Community

Discussion on: Improve your life with TDD

Collapse
 
vdedodev profile image
Vincent Dedo

How do you do design with TDD? That's one of the things that doesn't make sense to me.

Thread Thread
 
franiglesias profile image
Fran Iglesias

For example, imagine I need to write a new use case for an application.

I first start TDD'ing the use case as if it would be the unique class I'm gonna need.

Usually I start testing expecting exceptions because they usually are the simplest, but this may vary. I look for the simplest behavior. Sometimes this is very straight, some times it is a bit difficult to find it.

With every new test I may discover that I need collaborators for my use case in order to get data or services. Maybe it is a repository that I will need to double for testing, maybe it is some kind of service that already exists in the code, so I usually double it also. Sometimes I prefer use it as is.

Test by test I write the needed code to resolve my problem, and when I'm in the refactor phase I extract parts to private methods in the use case, that can lead me to extract some behavior to a new collaborator. This new services and collaborators are covered by the initial use case tests, so I don't need to separately test or add them (the collaborate with the Use Case to perform the behavior). I will add tests if I want to be more confident about them.

Of course, depending on the outcome of the UseCase (Is it a query returning something or is it a command?) I will need to test for a response or for a side effect, maybe I need to use a mock to verify some behavior.

Hope that helps.

Thread Thread
 
franiglesias profile image
Fran Iglesias • Edited

By the way, this post by Uncle Bob is very interesting to understand this approach:

blog.cleancoder.com/uncle-bob/2017...

Thread Thread
 
vdedodev profile image
Vincent Dedo

That's completely new to me in terms of TDD, but then again it doesn't sound like TDD and more decoupling. What about unit tests?

Thread Thread
 
franiglesias profile image
Fran Iglesias

I'm not sure if I'm explaining well the methodology, but maybe you can see it in action in this example (text is in Spanish, but I think you can follow the code samples to get the overall idea)

leanpub.com/testingytddparaphp/rea...