Skip to content

Setting up Test Driven Development (TDD) with Ruby on Rails and MAC OS X Lion

September 1, 2011

Have you ever gone through the Ruby on Rails Tutorial (www.railstutorial.org) by Michael Hartl?

It’s a great way to get your feet wet and start developing an application with RoR using your Mac OS X or Linux machine. The tutorial goes through a variety of RoR features, such as scaffolding, development of different controls, views and models, style sheets, security, resource creation, GitHub repositories, pushing your app to Heroku, among others.

The most interesting topic for me though was how easily test driven development (TDD) can be incorporated into your methodology, using free tools such as Rspec and Spork. It really is super powerful, especially for hacks such as myself, because your code is constantly being tested as you go. The coolest part of TDD is that it forces you to think about the results that you need before you actually write any code, because your first step is to write a failing test, which in turn forces you to think about use cases.

How TDD works

I found the following flow chart on Wikipedia and have seen the same chart in blog posts for different types of programming languages, which helped me understand what the thought process should look like.
As you can see in the flowchart, the steps are pretty self explanatory:

Add a test: the test should fail because you don’t have any code written. If it doesn’t fail either your test isn’t set up correctly or you have some code somewhere that allows it to pass. (Yeah, it happens).

Write code so that your test passes: write your code so that the test passes. Since this is your first attempt at writing code so that the test passes, don’t worry about having your code perfect. By the way it’s always a good idea to add comments of what your methods do, its also helpful to reference what methods are being tested and where.

Automate testing: the test should run every time you make a change and save your code. If you are using Spork, you may have to restart the server if you save your *.rb files, especially routes.rb.

Refactor: this is the step when you make your code more efficient and eliminate duplicates. For example, method one may do some of what method two does, if you put them together your code will be easier to maintain. You can be relatively confident that as you refactor your code is ok as long as your tests pass.

Another good resource is the book “Test Driven Development: By Example” by Kent Beck, which also clarifies the steps you need to take to write code using TDD:

1. Never write a single line of code unless you have a failing automated test.
2. Eliminate duplication.

TDD with RoR

Bringing this down to Ruby on Rails, first you have to set up Rspec, Autotest and Spork. The Rails Tutorial (www.railstutorial.org) offers detailed steps on how to get set up, here are the basics:

Install Autotest:

$ [sudo] gem install autotest -v 4.4.6
$ [sudo] gem install autotest-rails-pure -v 4.1.2

Install FS Event and Growl:

$ [sudo] gem install autotest-fsevent -v 0.2.4
$ [sudo] gem install autotest-growl -v 0.2.9

Create .autotest file in you application root

$ mate .autotest

require ‘autotest/growl’
require ‘autotest/fsevent’

Run Autotest:

$ autotest

Install Spork:

Spork makes your testing more efficient. If you use the command Rspec every time you run your test, it loads the whole rails environment every time you run your test…that can get really heavy. The Spork server loads rails once, from that point forward you just run the tests automatically with auto test and don’t have to worry about the additional load that much. Plus your tests will run faster.

Modify your Gemfile and run bundle install:

$ mate .Gemfile

source ‘http://rubygems.org’

gem ‘rails’, ’3.0.9′
gem ‘sqlite3′, ’1.3.3′

group :development do
gem ‘rspec-rails’, ’2.6.1′
end

group :test do
gem ‘rspec-rails’, ’2.6.1′
.
.
.
gem ‘spork’, ’0.9.0.rc8′
end

$ bundle install

Bootstrap Spork configuration:

bundle exec spork –bootstrap

Configure RSpec configuration file

Edit spec/spec_helper.rb, so that Rails environment gets loaded only once in pre fork configuration block:

require ‘spork’

Spork.prefork do
# Loading more in this block will cause your tests to run faster. However,
# if you change any configuration or code from libraries loaded here, you’ll
# need to restart spork for it take effect.
ENV["RAILS_ENV"] ||= ‘test’
require File.expand_path(“../../config/environment”, __FILE__)
require ‘rspec/rails’

# Requires supporting files with custom matchers and macros, etc,
# in ./support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

RSpec.configure do |config|
# == Mock Framework
#
# If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
#
# config.mock_with :mocha
# config.mock_with :flexmock
# config.mock_with :rr
config.mock_with :rspec

config.fixture_path = “#{::Rails.root}/spec/fixtures”

# If you’re not using ActiveRecord, or you’d prefer not to run each of your
# examples within a transaction, comment the following line or assign false
# instead of true.
config.use_transactional_fixtures = true
end
end

Spork.each_run do
end

Run Spork, by default it’ll run on port 8989:

$ bundle exec spork

That’s it. Start writing your failing tests, code and refactor, see how it goes!

About these ads
One Comment

Trackbacks & Pingbacks

  1. Macintosh tdd | Bgmiami

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 79 other followers

%d bloggers like this: