RSpec & Co — Test environment

I don’t care if it works on your machine! We are not shipping your machine!” — Vidiu Platon

This quote reminds the first time something went wrong with one of my application on Heroku. After testing the application locally in the browser, I was confident enough to push some code in production. When I visited the website, it wasn’t working. Everything was so perfect in the development environment… why it wasn’t the same in production? I have started to investigate and found a NoMethod error.

After fixing the bug I was asking myself “What can be done to avoid this type of situation in the future?”. The answer was quite simple, test the code before pushing it in production. Since then, I have started to learn RSpec and Co, and would like to share what I have learnt so far. In this post, we are going to set up our testing environment.

We are going to create a new application and name it Book Review. Our users will be able to write reviews about books they have read.

Terminal commands

-T' means we are not going to use Unit tests of Rails

Now we need to add few gems to the gemfile

let’s see what we just added :

  • rspec-rails : RSpec is a behavior-driven development (BDD) framework for the Ruby programming language. By adding rspec-rails we have extended RSpec and Ruby on Rails. rspec-rails will replace the rails testing subsystem (Unit Test)
  • spring-commands-rspec : If you take a closer look at your gemfile you can notice that the gem spring is already installed. Spring speeds up development by keeping your application running in the background so you don't need to boot it every time you run a rake task or migration. By adding the gem 'spring-commands-rspec' we are going to speed up our tests.
  • guard-rspec : It allows to automatically & intelligently launch specs when files are modified. The reason we added require: false is that the gem is going to be invoke manually.
  • capybara : Capybara helps you test web applications by simulating how a real user would interact with your app

We need to install our gems and connect spring with rspec.

Terminal commands

Now let’s install rspec by running the following command :

Terminal commands

The previous command created few files in our rails application :

rspec-rails and spring-commands-rspecare now installed, le'ts initialize guard with rspec :

Terminal commands

We just created the guardfile with the previous command. Open the file in your text editor and take a look at this line of code.

In other to use spring with guard, we need to change the cmd line :

Now let’s take advantage of few other gems : factory_girl_rails, database_cleaner and shoulda-matchers.


  • factory_girl_rails : it’s a fixtures replacement with a straightforward definition syntax. We are going to use it in all our tests (Features test, controllers test and model tests).
  • database_cleaner : Database Cleaner is a set of strategies for cleaning your database in Ruby. After each test, our test database will be empty.
  • shoulda-matchers: This gem provides matchers to test our models. It is very useful and easy to implement.

We can run bundle install in the command line to install these new gems 
For some reasons, you may encountered some difficulties to use should-matchers with spring. In order to use both gem, we need to require shoulda_matchers in the rails helper file : 
After the line require 'rspec/rails' we are going to add these lines of code :

Let’s include now database cleaner in the rails helper :

Take a look at the following line :

It means all ruby files in the support folder will be included in the rails_helper. So we going to create the folder and file needed in the command line :

Terminal commands


Our testing environment is now set up. You can browse the repository I created for this post : Bookreview repository

Just to be curious, do you add any other gems regarding your testing environment ? I am always willing to learn new things or improve the way iam coding so don’t be afraid to leave a comment!