The best kittens, technology, and video games blog in the world.

Saturday, May 07, 2016

Test-Driven Development for Optimists

Happy cat by loicdupasquier from flickr (CC-NC-ND)

If you do proper Test-Driven Development, writing your tests first, then your machine will call your code - and by extension you as well -a  failure. Over and over, every day you code, and use nasty red color for it just to drive the message:

Failures:

  1) HelloWorld says hello
     Failure/Error: expect(hello).to eq("Hello, world!")

     NameError:
       undefined local variable or method `hello' for #<RSpec::ExampleGroups::HelloWorld>
     # ./spec/hello_spec.rb:3:in `block (2 levels) in '

Finished in 0.00054 seconds (files took 0.13016 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/hello_spec.rb:2 # HelloWorld says hello

This kind of endless negativity cannot possibly be good for anyone - and you'll get it not just for minor mistakes you make, you'll get it for every single test. No wonder people burn out, switch to test-last development, or start making excuses for not writing tests:


Fortunately it's mostly just a matter of phrasing. Thanks to gem install rspec-half_full and setting up proper .rspec:

--color
--require rspec/half_full
--format RSpec::HalfFull

You can enjoy much more positive attitude:

Future successes:

  1) HelloWorld says hello
     Future Success: expect(hello).to eq("Hello, world!")
     NameError:
       undefined local variable or method `hello' for #<RSpec::ExampleGroups::HelloWorld>
     # ./spec/hello_spec.rb:3:in `block (2 levels) in '

Finished in 0.00084 seconds
1 example, 1 future success

Rerun on next step to success:

rspec ./spec/hello_spec.rb:2 # HelloWorld says hello

And not only is this psychologically healthier, it's also more accurate - specs you're writing are not failures, they're just description of future successful code you're planning to write.

Hopefully it will make you at least a little bit happier.

No comments: