Testing is an important aspect in 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 helps to write automated tests. RSpec, Capybara, Cucumber are some of the frameworks that we personally would like to talk about and explore.

RSpec :

RSpec is one of the frameworks that assists 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 test known as Example Group.

It : It is a keyword used to describe a Example or test, it accepts class name and string arguments.

Expect : Expect is a keyword used to define a expectation, expectation means to check whether certain condition is met .

Setting up RSpec would need the following approach .

  1. Add gem “rspec-rails” in your gem file and bundle install.
  2. Write your specs.
Rails RSpec Gem

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

Rails RSpec

Capybara :

Capybara is a 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 .

visit(‘url’)

fill_in “Email”

find(‘path’).click

click_button(‘button’)

CUCUMBER :

Cucumber is divided into 3 parts .

  1. Feature
  2. Scenarios
  3. Steps

Feature is divided into scenarios and scenarios is further divided into steps

  1. Feature : Feature is a use case that describe a specific function of the software being tested.
  2. Scenarios : A single scenario is a flow of events through the feature with an executable test cases for the system.
  3. Steps : A sequence of steps forms a scenario there are some of 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

gem ‘database_cleaner’

end

To execute it you can run command rails generate cucumber:install

Running cucumber with Rake:

rake cucumber

Running cucumber without rake:

[bundle exec] cucumber

Rails Cucumber

Create : Create keyword 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 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 which powers the cucumber integration testing framework that uses a webkit without UI. This allows to run tests in browser for automated testing. This is helpful in both development and testing.

Cucumber : Cucumber is a integration testing framework, it uses plain english like syntax to write the tests which allows writing automated tests. Cucumber follows a Behavior Driven Development model which describes what a web application should do in plain english.

 

At Intentwise, we provide SAAS software that helps with AMS  Reporting, AMS Analytics, and AMS recommendations based on our machine learning based recommendation engine. The Intentwise tool will help maximize your PPC returns while optimizing spend. If you are interested in trying out the tool that supports Amazon’s AMS automation, schedule a Demo now!