cipele46/cipele46-web — pull request #4 by shime (2013-07-22 14:34:10+00:00)
Title: Fast tests

We should embrace and enforce TDD!

It means you write tests before you write code. It means you run unit
tests before you make any change. It means slow tests slow you down and
might even do you harm. If they're slow it's a pain to run them.

A test is a unit test if it tests something in an isolation.

A test touching a DB is an integration test.
A test requiring ActiveRecord is an integration test.
A test requiring Rails is an integration test.

A test requiring only a class that encapsulates your business logic is a
unit test.

Each of your unit tests should be independent. Order should not matter.
Isolation encourages focus.

10 seconds wait for unit tests is **UNACCEPTABLE**!

Unit tests should be:
* Thorough
* Stable
* Fast
* Few

What have I done:

* removed dependency on spec_helper which loads up the Rails env, which
which adds about 10s to any test that's being ran
* removed pending unit tests because they only require spec_helper and
therefore just add to the boot time
* externalized business logic into modules located in /lib/extensions

-----

*"Give me some numbers already!"*

### Before

time rspec spec/models
rspec spec/models 8.10s user 0.58s system 57% cpu 14.982 total

### After

time rspec spec/models
rspec spec/models 0.44s user 0.05s system 99% cpu 0.500 total

So basically, tests are 30 times faster now.

------

I'm not pulling all these arguments out of thin air. Here are the resources:

* Sandi Metz - Magic Tricks for Testing : https://speakerdeck.com/skmetz/magic-tricks-of-testing-railsconf
* Sandi Metz - Solid Object Oriented Design : https://speakerdeck.com/skmetz/solid-object-oriented-design
* Avdi Grimm - Objects on Rails : http://objectsonrails.com/
* Corey Haines - Fast Rails Tests : http://arrrrcamp.be/videos/corey-haines/fast-rails-tests
* Tom Clemens - 4 Steps to Faster Rails Tests : http://tom-clements.com/blog/2011/10/23/4-steps-to-faster-rails-tests/

------

Thoughts? Opinions? Good to merge?