Search code examples
tdd

TDD FIRST principle


I am not understanding how the TDD FIRST principle isn't being adhered to in the following code.

These are my notes about the FIRST principle:

  • Fast: run (subset of) tests quickly (since you'll be running them all the time)
  • Independent: no tests depend on others, so can run any subset in any order
  • Repeatable: run N times, get same result (to help isolate bugs and enable automation)
  • Self-checking: test can automatically detect if passed (no human checking of output)
  • Timely: written about the same time as code under test (with TDD, written first!)

The quiz question:

Sally wants her website to have a special layout on the first Tuesday of every month. She has the following controller and test code:

# HomeController
 def index
   if Time.now.tuesday?
     render 'special_index'
   else
     render 'index'
   end
 end

 # HomeControllerSpec
 it "should render special template on Tuesdays" do
   get 'index'
   if Time.now.tuesday?
     response.should render_template('special_index')
   else
     response.should render_template('index')
   end
 end

What FIRST principle is not being followed?

  1. Fast
  2. Independent
  3. Repeatable
  4. Self-checking
  5. Timely

I'm not sure which FIRST principle is not being adhered to:

  • Fast: The code seems to be fast because there is nothing complex about its tests.
  • Independent: The test doesn't depend on other tests.
  • Repeatable: The test will get the same result every time. 'special_index' if it's Tuesday and 'index' if it's not Tuesday.
  • Self-checking: The test can automatically detect if it's passed.
  • Timely: Both the code and the test code are presented here at the same time.

I chose Timely on the quiz because the test code was presented after the controller code. But I got the question wrong, and in retrospect, this wasn't a good choice. I'm not sure which FIRST principle isn't being followed here.


Solution

  • It's not Repeatable as not everyday is Tuesday :) If you run this test on Monday you will get one result, if you run it on Tuesday, a different one.