Rails: RSpec and Capybara by Example

Article presents how to setup and use two popular gems used in tests in Rails: RSpec and Capybara. Post’s example based on TDD approach.

Post based on example application to get context in testing and its purpose is a managing library with books. In this article I focus only on the adding new books feature.

 

Setup Project

 

Ruby version: ruby 2.2.2p95
Rails version: rails 4.2.6

 

Let’s start by creating project:

If you prefer to use NO-SQL database, use following command:

 

If you choose MySQL database, you need to setup database access: username and password. It’s better to keep this data secret, so I use config/secrets.yml. Add to this file 2 key-value pairs and replace root and password_to_database:

Open config/database.yml and add just created secret keys:

Before you push any changes to repository, tell GIT to ignore our secrets. Open .gitignore file:

Add files to ignore:

And finally create database:

Let’s run application:

If you run application on external server (like VPS), probably you will need to run like below by adding IP and port:

Open application in web browser. If everything is OK, you should see following screen:

 

Rails_Home_scren_default

 

 

Test Gems

  • RSpec – testing framework for Rails
  • Capybara – to testing web pages

 

Installing Gems

Add to Gemfile:

Run:

Second command should create spec/spec_helper.rb and spec/rails_helper.rb files. Add following to spec/rails_helper.rb:

 

Testing: RSpec and Capybara

The example application is a CMS-like application to managing library with books. First feature will be the adding new books to library.

I start from defining a scenario for this feature’s test:

Next, I’ll implement it.

I create folder spec/books and file spec/books/creating_book_spec.rb:

To run all test:

To run specific test:

The test for creating book should failed, because nothing were implemented yet:

tdd_rails_failure

 

Currently, the test fails due to root_path ” / “.

I’ll try to pass this test. To do that, I have to create new model Book, controller with (at least) index, new, create actions and views for specific actions. I have too set the route path to Book Index action in routes.rb. 

I add required actions to app/controllers/book_controller.rb:

I set routes in config/routes.rb:

Create views for Book index and new actions: app/views/books/index.html.erb and  app/views/books/new.html.erb. Before I implement actions and views, I’ll run a test again:

tdd_rails_failure2

This time the test fails because it couldn’t find a button “Add New Book”. It’s (2) step in our test scenario. I’ll add the “Add New Book” button on index page (app/views/books/index.html.erb):

In web browser you should see link:

tdd_rails_3

Let’s run test again:

And as I expected, test passed 2nd step (link was found) and failed on 3rd step (it didn’t find a form):

tdd_rails_4

 

To pass 3rd step I need add form to app/views/books/new.html.erb:

Let’s open in web browser Home page and click on Add New Book link:

tdd_rails_3

It should redirect you on new.html.erb page where you should see simple following form:

tdd_rails_5

If you click on “Save Book” button, you should get error about “No route matches [POST] /books/new“, because the controller part is not ready. The same error you should get from test. So let’s add controller stuff to create new book and show it. To app/controller/books_controller.html.erb add following:

And add show.html.erb to app/views/books/ directory:

We should be able to add any book via web browser. Let’s check the test result:

The result:

tdd_rails_6

 

Success! Great!

 

The End

The article presents how to setup and use two popular test gems by example. Besides these two gems, you can find a few more gems like: Selenium Web Driver (useful especially in test of Javascript) and Factory Girl Rails (useful in managing test data). More about gems for tests in Rails I’ll try to present soon in another article.

 

Tomasz Antas

Ruby on Rails developer and Web designer.
The area of his interest includes Popular Science, Internet of Things, Wearables, AI and Virtual/Augmented Reality.

Latest posts by Tomasz Antas (see all)

Thanks for reading!

  • Frank Condezo Fabián

    Thanks for great article, God bless you.