Integration Testing with Rspec, Capybara

As a RoR (Ruby on Rails) developer, you probably know rspec – is a testing framework for Rails. Rspec helps us write tests for the controller, model, service. However, it only helps us check the accuracy of each method – it’s unit test.

We must write integration tests for our applications. We make sure the features of the system are work. For example, there is a “Login” page, I wonder if it is work. But don’t worry, there is a tool that helps us resolve them, that’s Capybara

Capybara

Capybara is an integration test framework. It helps you test web applications by simulating how a real user would interact with your app. It is agnostic about the driver running your tests and comes with Rack::Test and Selenium support built in. WebKit is supported through an external gem.

Setup

Capybara requires Ruby 2.3.0 or later. To install, add this line to your Gemfile and run bundle install:

Capybara with Rspec

In rails, you put Capybara specs in spec/features

For example, we start to test for the sign_in page:

Inside:

  • visit is a method to navigate to a page
  • within to restrict certain actions, such as interacting with forms or clicking links and buttons
  • fill_in to interacting with form elements
  • page is the page result of actions

feature is in fact just an alias for describe ..., type:> :feature, background is an alias for before, scenario for it, and given/given! aliases for let/let!, respectively.

Use js: true to switch to the Capybara.javascript_driver (:selenium by default), or provide a :driver option to switch to one specific driver.

To use selenium, you need to install selenium-webdriver gem. There are some seleniums as

  • :selenium => Selenium driving Firefox
  • :selenium_chrome => Selenium driving Chrome
  • :selenium_chrome_headless => Selenium driving Chrome in a headless configuration

Now, we use js: true for test sign_in page about, it’ll open browser chrome and step by step as our scenario

Some gem with capybara, and put them in test group

I shared the basic about Integration test with rspec and capybara. I hope it will be useful for you. You also can extend by reading more below the reference.

Thank you for reading and please comment below if you have any question or comment.

Thanks

Reference

https://github.com/teamcapybara/capybara

 

You may also like...