Testing a Simple Rails App Using Capybara and Rspec

By May 3, 2019 Tutorial

In this tutorial, I will give a brief overview of how to implement and run simple tests using Rspec and Capybara.

First, take a look at this beautiful cheat sheet for Capybara. Wow, that’s a lot of different things we can test with our new app! So where do we start? What do we test first? Let’s slow down a little, we have to get this whole testing situation set up first. The command “rails new” won’t help us too much in this regard, but it will get us started. If you’re not familiar with rails this will generate most of the files we need to run our app. The -T flag tells rails not to generate MiniTest tests.

rails new myapp -T

Then, let’s generate a user model, controller, routes, and some placeholder spec files.

rails g resource User name:string
rails g controller users index new show edit

Now let’s run our migration file.

rake db:migrate

Finally, we can get into setting up our testing suite. Let’s start by adding a few gems to our Gemfile.

gem 'rspec-rails'
gem 'capybara'

Next, we need to install our gems.

bundle install

Now, let’s generate our spec files.

rails g rspec:install

Now that we have generated these files we should have a file called rails_helper.rb in our spec folder. Let’s add the following line to this file inside the RSpec.configure do |config| code block.

 

config.include Capybara::DSL

This line configures Rspec to use Capybara syntax. Now let’s try to run our spec files.

rake spec

We’re going to get a lot of notifications in our console, they’re going to say we have several pending tests. This is because we haven’t written our tests, the test files generated are meant as placeholders until we get to that. Open up ‘/spec/models/user_spec.rb’ and paste in the following code.

require 'rails_helper'

RSpec.describe User, type: :model do
  it 'has a name field' do
    expect(User.new).to respond_to(:name)
  end
end

Notice, the only code we’re changing here is the code between the Rspec.describe block.

While we’re at it let’s delete the contents of our empty specs in our other spec files. Feel free to leave in the require statement and the block of code following it. Only delete the contents of the block.

Now let’s run our spec with:

rspec spec/models/user_spec.rb

Or if you cleaned up your spec files, we can use:

rake spec

This command will run all of the specs in our spec directory. It might not look like much happened because our spec passed! Now let’s add a failing test. Add this block into our user_spec.rb file:

it 'has a hat' do
  expect(User.new).to respond_to(:hat)
end

Now if we try to run our tests again we will see a failure message that looks something like this:

Failures:

  1) User has a hat
     Failure/Error: expect(User.new).to respond_to(:hat)

Nice! Now we can continue building out the rest of our app and adding more specs. Great job if you made it this far! If something went wrong while you were following along, drop a comment and I will respond quickly and help out!

One Comment

Leave a Reply