Day 37: Testing with Rspec and Capybara

Thursday, July 21, 2016

Topics

Testing

RSpec and Capybara

RSpec is a popular testing framework for the Ruby language, and is widely used for testing Rails applications. It can be considered a Domain-Specific Language (DSL), a computer language specialized to a particular application domain, and is written to resemble a natural language specification.

Cabybara is a web-based automation framework that simulates how users interact with your application, and is also a DSL. It lets us use high-level commands like #click_button or #visit to emulate how a user would actually use the application.

Using these two tools together, say we wanted to test that when a “User visits the home page successfully, we expect there to be an h1 element with text that says ‘Pokemon’”. This could be written as:

feature "User visits home page" do
  scenario "successfully" do
    visit root_path

    expect(page).to have_css "h1", text: "Pokemon"
  end
end

By adhering to the principles of Test-Driven Development (TDD), and writing this test before any code, we can follow the error messages of our failing test to write code that eventually meets the requirements and causes the test to pass. Once this first test passes, we can write a new spec for the next step of the user story and continue the process.

Project

We built a small sample app by following principles of TDD, utilizing RSpec and Capybara. Code can be found here:

Pokedex: Morning | Afternoon