Project Ramon

A learning journey from a Ruby noob perspective

Intro to Test Driven Development: Rspec & FactoryGirl


Today I had the pleasure of learning about TDD with Mark Simoneau and John Davis. Let me mention that before this week, I was using my browser as my only testing tool. I’m all too happy to announce that I’m slowly coming out of the stone-age and getting with the program… The Rspec & FactoryGirl program!

In today’s post, I will start off with a little teaser of todays sessions, and finish with an install and setup of Rspec and factory girl. Tomorrow I’ll go over the Rspec syntax in a little greater detail.

There is a ton of documentation on Rspec, some of it from prior versions and some of it looks forward to the future. I was very relieved when Mark pointed out that the syntax that I started using to test-drive my application could be updated to what the Rails community is beginning to embrace.

Here’s a quick example of the old syntax.

Mark mentioned that although the above syntax read easily on the developer side, that on the maintainers’ side the double equals == was a mess for them. So me-all for learning what will help me in the future-took Mark’s suggestion and updated the syntax. He introduced me to expectations, matchers, and the like.

It actually looks just as easy to read in my opinion. Check it out:

If you notice, in the it "has a url" spec on line 6, that instead of == false, we see our first expectation in the file which is passed articles’ save method as a parameter like so expect( be_false.

So now that we have had an Rspec appetizer of sorts, lets get into setting up the gems necessary to make our TDD a breeze!

Rspec & FactoryGirl Setup

step1: Install the rspec-rails & factory_girl gems

You’ll want to head on over to your Gemfile and setup an environment group for test and development if its not already there.

Our finished result should look something like this:


Notice that I have added the cucumber gem as well as pry-rails. John mentioned that in his experience, cucumber was not something that he typically encountered as he is an entrepreneur coder. He mentioned that if one were to work at a dev shop such as Thoughtbot or someplace similar, that cucumber scenarios would be more likely included as standard.

John also shared the pry-rails gem which is  a fantastic tool for debugging my code. In my case it also serves to help me visualize what goes on in Ruby/Rails in the background, which is assists me during this process of putting the web development pieces together.

So for our purposes the only two gems to include would be 'rspec-rails', '2.0' and 'factory-girl which is a fantastic gem to replace fixtures. FactoryGirl  was originally written by Joe Ferris and is now maintained by Josh Clayton over at Thouthbot.

We have our Gemfile saved, so we can run bundle install in our terminal.

Step 2: Setting up Rspec

The first thing we want to do is to install Rspec, we can head on over to our terminal and enter this command. rails generate rspec:install

This creates a spec folder in our application tree. The rspec-rails gem on github has a paragraph instructing us on how to utilize our rspec generators once installed.


Once installed, RSpec will generate spec files instead of Test::Unit test files when commands like rails generate model and rails generate controller are used.

You may also invoke RSpec generators independently. For instance, running rails generate rspec:model will generate a model spec. For more information, see list of all generators.

So far I have only done model tests, so lets run rails generate rspec:model Image title desc author img:binary to get our image_spec.rb folder established. Note that I have passed in attributes to the generator so I wouldn’t have to add them manually via another migration or set of migrations. Also note how any attributes with a data-type of text doesn’t need to be explicitly set, as Rails default attributes to a text data-type.

Here’s what our command above could look like:


Now that we have our specs in place, lets get into our next step.

Step3: FactoryGirl Setup

All we have to do to get our first factory set up is to create a new Ruby file in the factories folder called images.rb. Once our file has been created under the factories directory, we can start to set things up.

Take a look at how we can start to build our first factory:


Starting with line two you can see the symbol :image. This will always match your file name in a singular sense, and is the actual boundaries of where our factory data will be held.

Next we list all the attributes of the Image model, along with some generic data to populate our tests with.

Here’s a peak at our :image factory with its attributes set to some generic data:


We are now ready for tomorrow’s post, where we go overs some basic Rspec commands.

Hope to see you there!

Here’s my session with Mark Simoneau:


Categories: AirPair, Newbie, Ruby on rails

Tags: , , , ,

2 replies

  1. This is the best post I have read online thus far on rspec and factorygirl


  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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s