Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Rails Testing with Cucumber

    • 1
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 528
    Comment on it

    Rails Testing with Cucumber

    Hi Friends,

    In my previous blog Testing In Rails: Introduction and Creating Test Data, I had given you a brief idea about testing and later we talked about how we can test rails models and controllers. Here again I am going to talk about one more way of testing. which is using cucumber. Now before going directly to the topic lets refresh your mind regarding testing.

    Software testing:

    Checking our code for potential flaws that may result unwanted outcomes.

    Why test our code?

    We all make mistakes at some point of time while developing functionality. Some mistakes are minor and don't affect much. On the other hand some mistakes are too dangerous and can result severe security bugs. Some bugs are introduced while adding new functionality. Sometimes the bugs are not quite visible. So we need to test our code before pushing it live.

    Software testing is very important because of the following reasons:

    • Introducing new functionality can break old functionality while development phase.
    • To improve the quality of product.
    • For customer satisfaction. 
    • Properly tested application may require low maintenance cost. Results are accurate.

    Generally developers write test cases when all functionalities has been done. This can lead to ineffective testing as developer may not rememeber all the cases in the end due to high complexity. Writing all tests in the end may be quick but less effective. Test cases should be written alongwith development phase. So BDD (Behavior Driven Development) was introduced to over this issue.
     

    Cucumber:


    Cucumber is a very handy gem for testing rails applications. It supports BDD testing and uses plain english syntaxes and scenarios so that anybody can understand regardless of their technical knowledge. It uses simple Given-When-Then word to describe the process. Lets see this with a simple example:

    Given :
    I have some funds in my account:

    When :
    I transfer some balance to my other account B

    And the details are correct for account B

    And transaction password is correct

    And I press or click send button

    Then:
    Amount should be transferred to my B account
    And the source account should be debited with same amount

    Advantages For Using Cucumber:

    Its free. Customer can easily understand the process. It needs both tester and developer work together. Cucumber supports only web environment

    Using Cucumber:

    First Add cucumber and database clearner gems to Gemfile and then run bundle install:

    
    group :test, :development do
      gem 'cucumber-rails', :require => false
      gem 'database_cleaner' # database_cleaner is not required
    end
    

    Now install the cucumber using:

    rails generate cucumber:install


    Running Cucumber:

    Cucumber is run using rake like:

    rake cucumber

    Note: By default, cucumber-rails runs DatabaseCleaner.start and DatabaseCleaner.clean before and after test scenarios. We can disable this behaviour.

    Creating your tests:

    On your Rails project, Cucumber tests stay at features folder with .feature extension files. Lets create a feature file contact.feature for testing:

    Feature: Contact me
      In order to get in contact
      A visitor to our site
      Should send us a message using our contact form
     
      Scenario: contact message
        Given visiter is on the contact page
        And he fills in "name" with "George"
        And he fills in "email" with "george@gmail.com"
        And he fills in query with "i need assistance"
        When he presses "Send"
        Then page should have notice message "message successfully delivered."
    

    Since we have created only feature file, when we run this file it will give warnings of undefined steps. So now we need to create steps to handle the navigation. We create a navigation_steps.rb inside features/step_definitions/ folder.
     

    Given /^visiter is on (.+)$/ do |page_name|
      visit '/contact'
    end
    
    When /^he presses "([^\"]*)"$/ do |button|
      click_button(button)
    end
    
    When /^he fills in "([^\"]*)" with "([^\"]*)"$/ do |field, value|
      fill_in(field.gsub(' ', '_'), :with => value)
    end
    
    Then /^page should have notice message "([^\"]*)"$/ do |type, text|
      page.has_css?("p.#{type}", :text => text, :visible => true)
    end
    

    Now when we run tests again, all test passes.

    You can interact with the webapp by following links and buttons. Capybara automatically follows any redirects, and submits forms associated with buttons.
     

    click_link('id-of-link')
    click_link('Link Text')
    click_button('Save')
    click_on('Link Text') # clicks on either links or buttons
    click_on('Button Value')


    Interacting with forms:
    There are a number of tools for interacting with form elements:
     

    fill_in('First Name', :with => 'John')
    fill_in('Password', :with => 'Seekrit')
    fill_in('Description', :with => 'Really Long Text...')
    choose('A Radio Button')
    check('A Checkbox')
    uncheck('A Checkbox')
    attach_file('Image', '/path/to/image.jpg')
    select('Option', :from => 'Select Box')


    Hope you like this blog. For more like this please Click Here.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: