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?
- Fast
- Independent
- Repeatable
- Self-checking
- Timely
I'm not sure which FIRST principle is not being adhered to:
'special_index'
if it's Tuesday and 'index'
if it's not Tuesday.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.
In TDD, you write your unit test first, watch it fail, and then implement code changes until the test passes.
Test-driven development is the practice of developing software by first writing tests and then producing the minimum amount of code required to pass those tests. That statement pretty much captures it: you write clear requirements then you write enough code to meet them, and nothing more.
Each test should set up its own data and should not depend on any external factors to run its test.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With