Make the First Test Pass with “Good Morning”

We now have a failing test. The second step of TDD is to get this test to pass. At this point, people new to TDD often start adding code to examine the time argument of greet(time:). When you haven’t yet built up muscle memory of the TDD steps, it’s easy to fall back on old habits.

No, our job for the second step is to get the test to pass, and that’s all. The passing code can look silly and unsophisticated. The important thing is to avoid adding code that goes beyond the current set of tests. By keeping the production code from running beyond what the tests ask for, we are allowing the tests to be in control.

The test wants the function to return “Good morning” Here’s the quickest way to do that:

TDD/TDD/Greeter.swift
 func​ ​greet​(time: ​Date​) -> ​String​ {
»return​ ​"Good morning."
 }

This may bother you because you know the code is incomplete. Where’s “Good afternoon” or “Good evening”? Those will come as we add tests that expect those results.

images/aside-icons/tip.png

The nagging feeling that the production code is incomplete is a signal that you have more tests to write.

“Incomplete” is not the same as “incorrect.” Our greeter now works correctly—as long as you give it no name and call it during the morning hours.