Making TDD easier in Ruby by automating your tests

I’m a fan of test driven development, in order to speed up my turnaround time from writing functionality to testing the functionality, I use Guard( tool to automatically run my test any time I make a change.

This greatly speeds up my development time and hopefully it’ll help you too.

This is going to be our final files and folder structure.


##Dependencies Setup

First step is to setup a gem file with the dependencies that we will be using.

Create a new file named ‘Gemfile’ in your main project directory with the following contents

source ""

gem "rspec"

group :development do
gem 'guard'
gem 'guard-rspec'

Once done, run the bundle install commind in the terminal to install the dependencies, like so.

bundle install

##Seting up Guard

Create a new file named ‘Guardfile’ with the following content


guard :rspec, cmd: "bundle exec rspec --color" do

# watch /lib/ files
watch(%r{^lib/(.+).rb$}) do |m|

# watch /spec/ files
watch(%r{^spec/(.+).rb$}) do |m|

This file configures the Guardfile tool to watch for any changes to ruby files within the lib and spec directories and runs rspec.

Note: We have taken this route of manually creating a Guardfile rather than running “guard init” so we more easily see what the guard file is doing. Guard init generates a lot of code that we won’t be using in this guide.

Finally we launch our Guard instance to start watching our files by running the following command in the terminal.


That’s it, hopefully this helps speeds up your development time.

Here is the completed repo (



Leave a Reply

Please log in using one of these methods to post your comment: Logo

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

Google+ photo

You are commenting using your Google+ 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