Tales Of TDD - Stressed And Always In A Hurry
Tags: unittests, tdd, testing, softwaredesign
Published: 2020-08-19 10:39:49

So I noticed that you completed that feature that we’ve been pair programming yesterday?

_Yes, I was a little bored yesterday evening. So I decided to continue working on it_

Great job!

_Thanks. I’m glad the feature is ready to go._

Definitely! I still have one question though. I’ve noticed that you added fifty lines of code to that controller method without adding any tests. What I also noticed is that you’ve added that new mapper class, but there aren’t any tests for that either.

_Uhm, yes, uhm … about that …_

Did you forget to check in the source files of the tests?

_Well, in a sense that’s true. Because I didn’t write any tests._

Oh.

_I know that we’ve been using a test-first approach to build up this feature. I just wanted to finish up the implementation for this user story. I knew what the remaining code had to do, so I just wrote it. Sure, it required some more lines than I initially anticipated, but hey, it’s finished now. We can move on to the next user story. So I already …_

Can I ask you another question?

_Sure._

Why are you in such a hurry?

_I’m not in a hurry!_

Well, to me it seems that you’re a bit pressed for time.

_I just wanted to move on to the next user story._

Can we talk some more about the user story that we’ve been working on yesterday?

_Sure._

You’ve added some code yesterday evening. How do you know that this code actually works?

_It works properly because I tested it._

Great! How did you exercise the code that you’ve added?

_By running the application and clicking around in the UI._

You’ve added some code that builds a URL, right?

_Indeed_

Did you execute the code inside this if-statement?

_I don’t know. How can I be sure of that?_

Well, you could have used some code coverage tool while running the application.

_That sounds complicated._

Or you could have added a breakpoint for every execution path. There are four if-statements, so you need five breakpoints.One for each if-statement and an additional one in case all four conditions evaluate to false. Then you can start the application in debug-mode and go through the UI until you’ve hit all five breakpoints.

_Well, that sounds like a lot of work. Debugging the application usually takes a lot of time._

You’re right. It does require a lot of work and focus. Did you verify all those cases yesterday?

_I don’t know for sure. I guess so._

What about the design of the code? How did you manage to get some feedback there?

_Uhm, I didn’t. I just added the code, and it looked just fine to me._

Do you mind we pick up where we left off yesterday? How about we try to add one test for verifying whether the URLis build correctly.

_Shouldn’t we get going on the next user story?_

We do have some time to add a single unit test, don’t we?

_I guess so._

Can you try to add the test?

_Sure._

— After a short while —

_So, now we have a test that verifies whether the URL is build correctly. It seems to pass as well. However, … it doesseem to involve a lot of code compared to the other test methods that we already have. There’s a lot of setup code and then there’s also this “hack” that we needed in order to get the test to pass._

How could we improve this?

_I’m not really sure …_

How about we just add another test for when the condition of that if-statement evaluates to true?

_OK. Let’s do that!_

— After another short while —

_The amount of setup code for these tests really starts to bother me. How about we just move the implementation for composing that URL to a separate class?_

What’s your reasoning?

_Then we don’t need the overhead of setting up the entire controller just for testing the implementation that builds theURL. Composing the URL is just some data in, and the URL comes out. We could more easily test this in isolation._

Excellent idea! Let’s try that. Can I make one suggestion?

_Sure._

How about we revert all the changes that we’ve made thus far?

_You want to throw away the unit tests that we just added? Why?_

For starters, I don’t really trust them.

_Why don’t you trust the tests that we’ve added? They do seem to work and pass correctly._

They sure do. However, do they fail correctly as well? What I mean by that is whether they fail for the right reason?

_Why shouldn’t they?_

We didn’t see them fail. Right?

_That’s true._

Let’s just revert our changes.

_Are you sure about that?_

Definitely. We’ve learned enough by experimenting. Let’s get rid of them.

_Done. What’s next?_

Let’s write a failing test for that class you’ve mentioned earlier. You know, the class which composes URL’s.

_So we’re back at writing our tests first?_

Yes, we are.

_Shouldn’t we just copy and paste the code from the controller method into a new class?_

In that case, we’re back to adding tests after the fact. Then we don’t know whether those tests would fail for the right reason or not.

_I guess so. So we’re going to rewrite the code again?_

Yes. You can consider it as a refactoring exercise. First we’re going to use TDD to drive the implementation of the _URLComposer_ class. After that, we’re going to remove these fifty lines of code in the controller method. Then finally, we’re going to add another failing test to the test suite of the controller. You know the drill.

_Let’s get started then._

— After a while —

_This code definitely looks a lot cleaner now. Good thing that we performed this refactoring. Otherwise, we wouldn’t have discovered those two bugs that existed in the untested implementation._

Yes indeed.

_I always have this feeling TDD slows me down too much. But not this time._

How come?

_I can’t really explain it. Usually, everything seems so obvious and simple to the point that it feels that we’re just wasting time. Then again, we’re not. We’ve now spent less time reimplementing the URL composition compared to the time I’ve spent yesterday evening. How come you never seem to be stressed out while working on a user story?_

Because I work as fast as I can, but as slow as I need to.

_So, let’s move to the next user story then!_

Almost. Now, regarding that mapper class you’ve added yesterday evening …