You already know why you have to test your code so it’s time to introduce 4 gems that are super useful when writing tests for your rails application.


RSpec is a testing framework that supports Behavior Driven Development for Ruby. What BDD means? It simply means that tests that you write focus on your code behavior by describing how it behaves from the participants perspective. By participants I mean other methods and classes.

Describe is a very important word here because we would be using it quite often. A simple test written with RSpec would look like this:

describe ClassName do
  describe "#method_name" do
    context "when we are using method in a given context" do
      it "does something" do
        expect(class_name_instance.method_name).to eq('some result')

So what happened here?

1. First describe covers testing class definition
2. Second describe describes testing method name. I used # prefix to specify that we are testing instance method. You would use . prefix when testing class method – it’s just for documentation purposes and does not affect test at all
3. The context tag introduces additional context just for the documentation purposes and better readability. For example, your method may behave differently if user object passed to it has admin role and differently when the role is member. You can get rid of context tag by updating it tag to say does something when passed user is an admin – it’s up to you which way would you use
4. The last tag, it describes behavior. So if your method returns user name you would use it "returns user name" – this clearly tells what your method does.

If you will sum up information from above points you will realize that we created a piece of a documentation for one of our methods. Tests are check if our code is working properly but also show how does it work

You will use RSpec for unit tests but how about acceptance tests? [put link here]


When talking about acceptance tests it’s time to talk about Capybara. Capybara helps you test web applications by simulating how a real user would interact with your app.

Since acceptance tests are for higher lever interactions you don’t test methods but scenarios. A scenario is a simple history that describes how your application should behave from the user perspective. I used to connect Capybara with RSpec so if you want to test your sign in form you may have such test:

feature "User signs in" do
  scenario "he sees successful notification" do
    visit users_sign_in_path
    fill_in "user_email", with: ""
    fill_in "user_password", with: "password"
    click_button "Sign in"
    expect(page).to have_content("You are signed in")

You can also use Capybara without RSpec but Capybara deserves for its own article. If you don’t want to miss it, consider signing up for the newsletter. For now, the difference between tests with pure RSpec and Capybara + RSpec is firmly visible.


It’s a ruby gem for setting up Ruby objects as test data. If you want to test things related to the database such as queries you may find this library useful. Let’s consider following class:

class UserQuery
  def initialize(user)
    @user = user
  def users_with_the_same_first_name
    User.where(first_name: user.first_name).where.not(id:
  attr_reader :user

Above class, it’s a simple query class. Method #users_with_the_same_first_name returns other users with the same name but excluding current user – it’s obvious. This time I didn’t apply to TDD rules intentionally and I wrote class first just to show why you may find FactoryBot gem useful.

Without using the gem, to create an object in a database you have to simply build the new object and then save it (or use .create! method – whatever you like or what is the best in given context):

it "returns users with the same name" do
  user = User.create!(first_name: "Tom", last_name: "White", email: "", enabled: true)
  user_query =
  similar_user = User.create!(first_name: "Tom", last_name: "Black", email: "", enabled: true)
  expect(user_query.users_with_the_same_first_name).to eq([similar_user])

In above test, we have to create two users in the database. Since we are creating two records using the same model some of the parts are common for both calls. We are passing enabled: true for both users but in a real application you may need to pass multiple arguments – this is the time when our gem comes on the stage. Also, it’s not fun to wonder what email you should give each time when you are creating a User object to not put already used email.

FactoryBot.define do
  sequence :email do |n|
  factory :user do
    enabled { true }

Above code is a definition of your first factory. For now, don’t focus on the syntax and just realize that you can separate commonly used actions to not repeat them each time you are creating given record. Our :user factory fill email attribute with user#{n} value where n is incremented each time the new factory is used so you don’t have to worry that you will duplicate email – tricky, isn’t it?

Let’s now rewrite our test this time using FactoryBot gem and our factory:

it "returns users with the same name" do
  user = FactoryGirl.create(:user, first_name: "Tom", last_name: "White")
  user_query =
  similar_user = FactoryGirl.create(:user, first_name: "Tom", last_name: "Black")
  expect(user_query.users_with_the_same_first_name).to eq([similar_user])

Of course, you can do much more but will take care of FactoryBot gem usage in future articles. For now, just know that something is happening.

Database Cleaner

The name of this gem is very meaningful and I think and don’t have to explain it. Database Cleaner cares about cleaning your database between your tests. Since each test tests something else or other cases we don’t want to have records in the database from the previous test – it may disturb our current test.

You can use this gem with many testing frameworks but you can also use it with RSpec! For now, it’s nice to know that there is someone who will do it the dirty work for us. We would back later to describe how to configure and use this gem to work well for us and keep our DB clean.

Go and test it

I hope you find those gems useful. If you have any gems that are absolutely must-have for you when you are testing code – let me know! If you are not sure about your tests yet, don’t hesitate and hit me on twitter or contact me, I would love to help you.

Download free RSpec & TDD ebook

Do you want to earn more or jump to the next level in your company? Do you know that testing skills are one of the most desired skills? There is only first step: start testing and do it right. My ebook can help you. Subscribe to the newsletter to get a free copy of the book.

One Comment

  1. Pingback: Are you using these 4 gems for testing your RoR application?

Leave a Comment

Your email address will not be published. Required fields are marked *