Getting Up And Running On Rails With RSpec and Capybara

Getting Up And Running On Rails With RSpec and CapybaraSpike BurtonBlockedUnblockFollowFollowingMar 5Photo by Karen Lau on UnsplashWriting tests for application code is something that all programmers ought to be familiar with doing, regardless of language or framework of choice.

RSpec is an incredible testing suite and DSL (Domain-Specific Language) built in Ruby, which allows you to write unit tests with ease using a set of pre-defined methods.

My introduction to writing code in Ruby was tightly entwined with the experience of religiously running RSpec to test what I had written; and so when I started learning Rails, continuing to use RSpec made for a more natural and seamless transition.

By default, Rails uses the MiniTest testing framework.

We will discuss how to set up a new Rails project to use RSpec, and then explore using Capybara to test our views.

First, we will create a new Rails project by running rails new NAME -T -B, where NAME is the path to the directory for the project that should be created.

The -T flag specifies that Rails should not set up any testing framework for our new project, which is exactly what we want.

The -B flag instructs Rails to skip running bundle install at this point.

Next, we need to edit our Gemfile to include the following:group :development, :test do gem 'rspec-rails' gem 'capybara'endThis adds both RSpec and Capybara to our development and test environments.

At this point, run bundle to install gems and any dependencies the project needs.

I like to drop into rails console at this point just to check that everything is working, and if not — I go back to my Gemfile and fix any issues.

Now that we have our project skeleton in place, the next step is to rig up our testing suite.

Simply run rails generate rspec:install at this point to create the spec directory and necessary file structure.

The last step is to tell our tests to use Capybara.

In spec/rails_helper.

rb, add the following:require 'capybara/rspec'That’s it for setup!.Before going any further, let’s commit changes we’ve made to our project: git add .

&& git commit -m "initial commit".

Let’s dive into creating a controller and doing some testing!Photo by Max Nelson on UnsplashFor our testing purposes, we will create a simple form with some inputs, and then tell Capybara to fill in some sample data and submit the form.

We won’t worry about creating any complex models or persisting information to a database.

Let’s tell Rails to create a users controller and setup some routing for us:rails generate controller Users new –no-helper –no-assets –no-view-specsBy specifying –no-helper –no-assets we are telling Rails not to create any assets or a helper for our controller.

The –no-view-specs option instructs the generator not to create any tests for the views — we will see later on how to create the tests we want.

Rails has created a new route for us, which we can see in config/routes.




draw do get 'users/new'endAnd in app/controllers/users_controller.

rb:class UsersController < ApplicationController def new endendFinally, Rails created a view for us in app/views/users/new.



Let’s edit this view to create a simple form:<h1>User Login</h1><%= form_tag users_path do %> <%= label_tag :name %> <%= text_field_tag :name %> <%= submit_tag "submit" %><% end %>We need to do a couple of things to finish wiring this up.

In routes.

rb add the following:post 'users' => 'users#welcome'We also need to add a welcome action to users_controller.

rb:def welcome @user = params[:name] render :welcomeendThe final step here is creating views/users/welcome.


erb and including some HTML and ERB to display the user’s name:<h1><%= "Welcome, #{@user}!" %></h1>Phew!.If we run rails server and go to localhost:3000/users/new in our browser, we should be greeted with a form asking us to input our name.

When we enter our name and press the submit button, we should be redirected to a page greeting us with a warm welcome.

This is all good and well, but having to test our code this way to make sure it works every time we add a new form or implement a new feature will soon become tiresome.

Luckily, a tool has been created to test and automate this process for us!This is where Capybara comes in.

Capybara is a framework which allows us to test the code in our views by filling in form data, inspecting page content and sending HTTP requests to the browser.

Let’s set up a spec to test our newly created form:rails generate rspec:feature formThis will generate the file spec/features/form_spec.


By default, Capybara expects to find specs in a directory called features.

Let’s edit this file to add a new test, which will fill in the name text field input and press submit:require 'rails_helper'RSpec.

feature "Forms", type: :feature do it 'can enter a name and receive a greeting' do visit 'users/new' fill_in :name, with: "Capybara" click_on 'submit' expect(page).

to have_content "Welcome, Capybara!" endendThat’s it!.We have successfully created our first test in Rails using RSpec and Capybara.

If we run rspec –format documentation, we should see that the test passes.

Feel free to experiment — what are ways to make the test fail?.Are there other tests we could create for this example?.We have only scratched the surface here — hopefully this gets you thinking about how you can simplify and automate the process of testing your code using these tools, and get into the habit of testing your code often.

Visit here for the code presented in this post.

.. More details

Leave a Reply