Project Ramon

A learning journey from a Ruby noob perspective

TDD your Ruby project: Ruby off Rails setup


I had another AirPair with Mark Simoneau last week. Its our last pair session as he has other engagements. I truly appreciate all of the direction, and instruction that he provided.

In our last session I took part in my first “ping pong pair”  with him and it was a great experience. It really aided in both learning TDD and understanding the data-model being built.

I wanted to pass along the knowledge of being able to understand Ruby Koans, there are a lot of assert methods that are involved. Today we’ll just cover how to setup a test structure for a Ruby project outside of Rails and go over some of the concepts of Test::Unit, which aren’t hardly any different from Rspec, in a follow-up post.

Ruby Project Setup

Ok first things first, lets create a new project. We should change directory to our new project folder where we typically store Ruby projects, for me its inside of a Ruby folder. If you don’t have a folder that holds your Ruby files you can create a folder by typing mkdir Ruby.

Since I already have a current folder that holds my Ruby project files. We can type cd Ruby
to get inside and mkdir new_project makes a folder to hold future files for our project. We can create our first project file by typing touch new_project.rb, and confirm that our file was created by typing ls to list all files in the current directory.

Here’s an example of what that looks like in the terminal:


Now we can get started setting up our gems. I use bundler to manage my dependencies, following the instructions here is our next step.

In the terminal we type gem install bundler to add this gem to our project. You should see something like the following if your install was successful:


Next we want to install the gems necessary to run Rspec for test-driving our project development. To do this we first need to create a Gemfile, and can make this happen by typing touch Gemfile. Once again you can type ls to ensure that the Gemfile has been created, and then open up the file to add the Rspec gem.


Heres a look at what should be placed inside the Gemfile:


Line 1 adds the source, and line 3 adds the Rspec gem. If you take a look here, the file under install, it shows that installing gem 'rspec' will add rspec-core, rspec-expectations and rspec-mocks. If we wanted only one or two of these we could install the specific gems one at a time in our Gemfile. Since in this case we want them all, we’ll just use the gem 'rspec' as you see in line 3 in the above illustration.

Once we’ve added the source and gem to our Gemfile, we’re going to want to run bundle install in our temrinal to fetch our dependencies. Heres an illustration of the results of a successful bundle install or bundle for short.


Note that after running bundle install or the synonymous bundle alias, our project contents has a new addition. Namely the Gemfile.lock file.


The Gemfile.lock is essentially a snapshot of current dependencies with exact versions of the gems we’re using. This StackOverflow post explains in greater detail.

Rspec setup

Running the command rspec spec returns a LoadError currently. This is because we have yet to create our test folder and files in our project tree yet. Lets take care of this now.

Following the same process as we did back in the section above, we’re going to make a spec folder and add a spec_helper.rb file and a new_project_spec.rbfile.

Here’s a quote from the Rspec documentation about the purpose of a spec_helper file:

It is conventional to put configuration in and require assorted support files
from spec/spec_helper.rb. It is also conventional to require that file from
the spec files using require ‘spec_helper’. This works because RSpec
implicitly adds the spec directory to the LOAD_PATH. It also adds lib, so
your implementation files will be on the LOAD_PATH as well.

In a nutshell, its a config file. We’ll be adding the following configurations to it.


These configs just add color to our Rspec terminal output, making our tests easier to read.

We need to add one more file in our new_project directory. The file will be called .rspec, and the dot .prepended to the file name .rspec means its a hidden file. You can read more about hidden files here.

Here’s an illustration from my terminal:


The command touch .rspec creates the hidden file for us, but notice how when I typed ls the file was not shown. In order to list all files in our directory, we can type the command ls -a to list all files.

Inside of our .rspec hidden file we can add --color to line 1 and save the file.


Now when we run rspec spec in our terminal, we should receive color-coded output like so:


Were finally all set up! In my next post we’ll cover how to understand Test::Unit and Rspec in a Ruby project, stay tuned.

Here is the video from my AirPair with Mark, check it out when you have some free time:


Categories: AirPair, Newbie, Ruby

Tags: , , ,

1 reply


  1. – What I’ve Learned pt. 2 | Project Ramon

Leave a Reply

Fill in your details below or click an icon to log in: 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