Here Are A Few Of My Favorite Gems (For Testing ;)

I absolutely LOVE the idea of Test Driven Development (TDD). Write a test, see it turn RED, write the code to make the test pass GREEN, REFACTOR your code, and keep doing it over and over again!

However, if you’ve tried writing tests, especially for Rails, you know how tedious the process could be to set up. I use Rspec for unit tests, Capybara for integration tests, Guard to keep re-running the tests every time I make changes to any files, and Spork  to have the tests run faster. And here are a few of my favorite gems that make developing and testing even more pleasant:

annotate

The annotate gem, which I first read about in the Ruby on Rails tutorial, adds you database schema as a comment in your models if you run “$ bundle exec annotate” after each database migration. AND, as an added bonus, if you run “$ bundle exec annotate -r” it will also annotate your routes (no more running “$ rake routes”!). Here is an example annotation of the user model in the current project I’m working on:

# == Schema Information
#
# Table name: users
#
#  id           :integer          not null, primary key
#  uid          :string(255)
#  name         :string(255)
#  image_url    :string(255)
#  twitter_name :string(255)
#  auth_token   :string(255)
#  auth_secret  :string(255)
#  created_at   :datetime         not null
#  updated_at   :datetime         not null
#

class User < ActiveRecord::Base
  attr_accessible :auth_secret, :auth_token, :image_url, :name, :twitter_name, :uid
  has_many :tweets
end

quiet_assets

The quiet_assets gem keeps your server log CLEAN by getting rid of all the messages about getting your assets to load. No more scrolling through the log!

faker

The faker gem makes it easy to fill in fake data when you Fabricate your models. Here is an example of how I’m currently using the Faker gem to Fabricate the User model in The Tweetbox (a project I’m currently working on with an amazing team!!!):

# spec/fabricators/user_fabricator.rb
Fabricator(:user) do
  uid           { (1..8).collect { rand(10) }.join }
  twitter_name  { Faker::Name.first_name } #faking a twitter username
  name          { Faker::Name.name } #faking a name
  auth_token    { (0...20).map{97.+(rand(25)).chr}.join }
  auth_secret   { (0...30).map{65.+(rand(25)).chr}.join }
  image_url     { Faker::Internet.domain_name } # faking a url
end

launchy

The launchy gem adds the method “save_and_open_page” to your Capybara integration test. Basically, the “save_and_open_page” method makes it easy to debug your code by saving the action Capybara is doing (e.g. filling in the username) and opening the saved version of the webpage with that action in your browser.

shoulda-matchers

The shoulda gem makes it really easy to test your default Rails validations. So, for example, if you want to add a validation that a User should have many Tweets, simply add this test your User model spec:

it { should have_many :tweets }

database-cleaner

The database-cleaner gem really came to the rescue for me today! This gem removes everything from your test database whenever you need it to. I have this gem clean the database right before my tests are run!

# part of my spec_helper.rb file
Spork.each_run do
  # This code will be run each time you run your specs.
  DatabaseCleaner.clean
end

All My Test Gems!

In case you’re curious, I’d like to share all the test / development gems from the project I’m currently working on. I’m sooo grateful to have all of these beautiful gems in my toolbox:

group :development, :test do
  gem 'rspec-rails', '2.10.0'
  gem 'annotate', '2.5.0'
  gem 'quiet_assets', '1.0.1'
  gem 'simplecov', '0.6.3'
  gem 'capybara', '1.1.2'
  gem 'fabrication', '2.2.2'
  gem 'launchy', '2.1.2'
  gem 'guard-rspec', '1.2.1'
  gem 'guard-spork', '0.3.2'
  gem 'spork', '0.9.0'
  gem 'faker'
  gem 'shoulda-matchers'
  gem 'database_cleaner'
end

Enjoy the article? Join over 14,500+ Swift developers and enthusiasts who get my weekly updates.