September 20th, 2017
Testing is an important aspect of software development. One of the approaches followed by many developers is Test-driven development (TDD). It’s the approach where you start with a test and build your code accordingly while improving your test coverage.
Whether you use TDD or not, almost all good developers agree that writing automated tests is and should be an important process of software development. Ruby on Rails has many testing frameworks which help to write automated tests. RSpec, Capybara, Cucumber are some of the frameworks that we personally would like to talk about and explore.
RSpec is one of the frameworks that assist you in using the BDD approach. Some of the key syntax used within RSpec are:
Describe: Describe is a keyword used to describe a collection of tests known as Example Group.
It: It is a keyword used to describe an Example or test, it accepts class name and string arguments.
Expect: Expect is a keyword used to define an expectation, expectation means to check whether a certain condition is met.
Setting up RSpec would need the following approach.
- Add gem “rspec-rails” in your gem file and bundle install.
- Write your specs.
3. The command to execute your specs is rspec or you can execute it through your IDE.
4. If you have to just test a single line say line 28 you can do something like this, rspec ./spec/features/user_registers_spec.rb:28
Capybara is an integration testing tool for ruby on rails, Capybara is built on webrat. Capybara integrates easily with RSpec.
Capybara Installation :
Add gem capybara in your gem file and bundle install
some of DSL of capybara looks like this.
Cucumber is divided into 3 parts.
The feature is divided into scenarios and scenarios is further divided into steps
- Feature: Feature is a use case that describes a specific function of the software being tested.
- Scenarios: A single scenario is a flow of events through the feature with executable test cases for the system.
- Steps: A sequence of steps forms a scenario there are some of the keywords they are, Given, where, then, and, but.
Cucumber installation :
Add the gem to your gem file as follows and do a bundle install
group :test do
gem ‘cucumber-rails’, :require => false
#database_cleaner is not required, but highly recommended
To execute it you can run command rails generate cucumber: install
Running cucumber with Rake:
Running cucumber without rake:
[bundle exec] cucumber
Create: Create a keyword that is used to create a new user with the given credentials.
Visit: After creating a new user you should be able to see the login page for which the path is mentioned using visit keyword.
Login: when you are in the login page you need to login in order to go inside the app for which you need to provide the login details which is email and password as shown in the screenshot.
Expect: once you are inside the app you are expected to see certain things that are mentioned. Since this is a test done for “unconfirmed user” you are expected to see contents like “you have to confirm your email address before continuing”.
Difference between RSpec, Capybara and Cucumber:
Rspec: RSpec is a framework that helps us to write unit tests, functional tests, and integration tests. These tests try to send requests to your rails application, but don’t actually run the application end-to-end.
Capybara: Capybara is a web driver that powers the cucumber integration testing framework that uses a WebKit without UI. This allows running tests in browser for automated testing. This is helpful in both development and testing.
Cucumber: Cucumber is an integration testing framework, it uses plain English like syntax to write the tests which allow writing automated tests. Cucumber follows a Behavior Driven Development model which describes what a web application should do in plain English.
Read our post “Rails Authentication Frameworks” for a short analysis of the various Ruby on Rails authentication frameworks including Cancan, Auth-logic, Devise, etc.
Intentwise is a Chicago-based technology company that helps brands, sellers, and agencies maximize returns from Amazon advertising spend. Intentwise’s industry-leading SaaS platform provides impactful recommendations and automation to accelerate advertising optimization while saving valuable time for advertisers.