Test Driven Development (TDD) – Part 1 – Setting up RSpec

“Write your tests first!”  We’ve all heard it, but when you’re new to programming (or even a seasoned professional) it can be tempting to speed through developing your features without taking the time to stop and create tests.

Recently, I went back to an app I built in August, with the intention of adding some new functionality.  Problem was, some of the existing features weren’t working as they should and seeing as I hadn’t written tests, debugging became such a nightmare that I eventually gave up and moved on to another project.

So I’m now a convert, a full fledged, card carrying member of Test Driven Development.  And now I’ll take you through some of the basics of testing your rails up so you don’t succumb to the same fate as I did:

Once you’ve generated your initial rails up, the first thing you’ll need to do is include the rspec gem in a :test group.  In your Gemfile add the following:

group :test, :development do
  gem 'rspec-rails'

Don’t forget to bundle after.  Next let’s create the necessary rspec folders by running rails generate rspec:install in the terminal.  This generates three things:

.rspec -a configuration file that you can use to personalize your rspec environment.

spec – folder that will eventually hold all of your tests.

spec/spec_helper.rb  - As the name suggests, this file helps tests run and will therefore need to be required in all of your test files.



Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>