BDD and Setting Up RSpec and Testing Suite

(This article assumes a rails app and database have been created and that the developer has an intermediate understanding of Rails).

Behavior-driven development (BDD) takes the position that you can turn an idea for an app or feature into product ready code that’s specific enough for both the developers and clients to understand what’s going on. There is a focus on creating tests that are written in natural language constructs, making it easier to identify what part of the website is not working.  It’s BDD that’s at the heart of using RSpec, which is a software testing tool for Ruby programmers that tests the functionality of an application.

For more on the origins of BDD see here.

The following explains how to install RSpec along with some other testing features that can be used in your Rails app.

Setting Up RSpec

To begin using RSpec you will need to add the latest RSpec gem to your gemfile.

gem 'rspec-rails', '~> 3.4'

Go to the terminal and run


Continuing in the terminal, run

rails generate rspec:install

This command creates a folder called spec in your Rails app. It’s in this folder where you’ll create your tests.

To run a test in the terminal navigate to your app’s folder and enter:


Setting Up Factories

If you want to use factories then you should install factory_girls_rails, which is recommended, but factories aren’t absolutely necessary for writing your tests. Factory_girls replaces Rails’s fixtures with factories. Factories have a more straightforward syntax, they support multiple build strategies (saved instances, unsaved instances, attribute hashes, and stubbed objects), and they have support for multiple factories in the same class (user,admin_user, and so on). This includes factory inheritance. It also automatically updates fixtures as you change your data models. You can find the gem here.

Gem Testers (Use is determined by situation)

The following is a list of a gems that can help you test your website. They can all be found by typing in there names in at

faker – generates fake data for your database. Not all data is unique.

capybara – helps with testing websites by interacting with it in a way that an actual user would interact with it.

database_cleaner – provides a list of strategies or commands that can be used to clean up a database

launchy – helps to create a common approach to launching applications designed in Ruby to multiple platforms, like emails or browsers.

selenium-webdriver – a tool that writes automated tests of websites. It mimics a users interaction with an application, with a specific focus on interacting with html.

Gem File Suite

IMPORTANT: As new versions for these gems are released, make sure to update to the most recent versions. The gem file suite with all the tests set up in it should look like this:

group :development, :test do
  gem 'rspec-rails', '~> 3.4'
  gem 'factory_girl_rails', '~> 4.6'

group :test do
  gem 'faker', '~> 1.6', '>= 1.6.3'
  gem 'capybara', '~> 2.6', '>= 2.6.2'
  gem 'database_cleaner', '~> 1.5', '>= 1.5.1'
  gem 'launchy', '~> 2.4', '>= 2.4.3'
  gem 'selenium-webdriver', '~> 2.53'


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