A Beginner's Guide to RSPEC

RSpec is a testing framework that has revolutionized the way Ruby on Rails developers write tests. It provides a powerful set of tools that enable developers to write expressive and maintainable tests, resulting in more reliable and higher quality code.

With RSpec, developers can write tests that are easy to read, understand, and modify, making it a preferred choice for developers around the world. In this article, we will explore the power of RSpec and how it can help developers to write better tests and build more robust applications.

So, if you are interested in learning how RSpec can improve your testing workflow and boost your productivity, read on!

Setting the Stage: Installing RSpec

First thing first, let's get that gem installed:

# Gemfile
group :development, :test do
  gem 'rspec-rails', '~> 5.0'

Now, in your terminal, don't forget to bundle that beautiful gem:

$ bundle install

And then this:

$ rails generate rspec:install

Settings successfully configured. You've successfully installed the beast that is RSpec – go celebrate with some coffee, you deserve it.

Folders and Files, Oh My!

One noteworthy thing that RSpec does is creating directories with the "_spec.rb" suffix. But, seriously, who cares about the file system, right? Anyway, you've got a few lovely default folders to work with:

  • spec/models: Where your model tests live.
  • spec/controllers: A nice abode for your controller test dwellers.
  • spec/mailers: When you want to rest assured that your email is being sent effectively.
  • spec/views: What, you want to test your views too? Mad lad!

Now let's move on to the pièce de résistance...

The First Taste of RSpec Goodness: Model Tests

Alright, you insatiable developer, here's an example of a simple yet brilliant model test.

# app/models/user.rb
class User < ApplicationRecord
  validates :name, presence: true, length: { minimum: 3 }

A simple, unassuming User model if there ever was one. Now for the test!

# spec/models/user_spec.rb
require 'rails_helper'

RSpec.describe User, type: :model do
  it 'is valid with a name' do
    user = User.new(name: 'Bartholomew')
    expect(user).to be_valid

  it 'is invalid without a name' do
    user = User.new(name: nil)
    expect(user).to_not be_valid

  it 'is invalid with a short name' do
    user = User.new(name: 'Ed')
    expect(user).to_not be_valid

And BAM! You've just written your first model test. May you forever have green tests.

Controller Tests: Keep Those Controllers in Line

What could possibly go wrong, right? Oh, trust me, plenty can go haywire with your controllers. So let's test 'em.

# app/controllers/users_controller.rb
class UsersController < ApplicationController
  def index
    @users = User.all

  def show
    @user = User.find(params[:id])

Nothing too crazy going on with our UsersController, but you never know when a slight change can wreak havoc.

# spec/controllers/users_controller_spec.rb
require 'rails_helper'

RSpec.describe UsersController, type: :controller do
  let!(:user) { User.create!(name: 'Gertrude') }

  describe 'GET #index' do
    it 'populates an array of users' do
      get :index
      expect(assigns(:users)).to eq([user])

  describe 'GET #show' do
    it 'assigns the requested user to @user' do
      get :show, params: { id: user.id }
      expect(assigns(:user)).to eq(user)

And with that, you've successfully thwarted that pesky ActiveRecord::RecordNotFound error.

Running Those Sweet Specs

Alright, you've made it this far. You must be dying to know if all those blood, sweat, and tears will pass the tests.

In your terminal, type these magic words:

$ rspec

"The moment of truth," you think to yourself. And then...


Finished in 0.13837 seconds (files took 2.12 seconds to load)
5 examples, 0 failures

Congratulations! Your tests have passed, and the warm green glow of a successful build fills your programmer heart with pride.

Now, get yourself back in there and keep testing everything in sight – after all, they say the devil is in the details (or lack thereof). Welcome to the RSpec magic show!