CSC/ECE 517 Fall 2012/ch1b 1w71 gs: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 15: Line 15:


  # Language: English
  # Language: English
  Feature: InterestCalc
  Feature: InterestCalc
  In order to avoid an error in interest calculation
  In order to avoid an error in interest calculation
  As a new student
  As a new student
  I want to be told the interest for given principal per year
  I want to be told the interest for given principal per year
   Scenario: Interest on Principle
   Scenario: Interest on Principle
   * I have entered 1000 into the interest_calc
   * I have entered 1000 into the interest_calc
Line 27: Line 25:
   * The result should 35 on the screen
   * The result should 35 on the screen


Step Definitions:
Step Definitions:


     begin require 'rpec/expectations';
     begin require 'rpec/expectations';
Line 36: Line 34:
     $:.unshift(File.dirname(_FILE_) + '/../../lib')
     $:.unshift(File.dirname(_FILE_) + '/../../lib')
     require 'Interest_calc'
     require 'Interest_calc'
     Given /I have entered (.*) into the interest_calc/ do |n|
     Given /I have entered (.*) into the interest_calc/ do |n|
       @calci=Interest_calc.new
       @calci=Interest_calc.new
       @calci.setdata(n)
       @calci.setdata(n)
     end
     end
     When /I press (\w+)/ do |op|
     When /I press (\w+)/ do |op|
       @result=@calci.calc
       @result=@calci.calc
     end
     end
     Then /the result should be (.*) on the screen/ do |result|
     Then /the result should be (.*) on the screen/ do |result|
       @result.should == result.to_f
       @result.should == result.to_f
     end
     end


Class def:  
Class def:  


   class Interest_calc
   class Interest_calc
Line 57: Line 52:
       @args<<n
       @args<<n
     end
     end
     def calc
     def calc
       @args.inject(0){|p,r| p=p*r/100}
       @args.inject(0){|p,r| p=p*r/100}
     end
     end
   end
   end

Revision as of 21:45, 3 October 2012

Cucumber and Capybara

Introduction

Cucumber is one of the latest additions to the RSpec family of tools for testing in Ruby. Testing is one of the most important parts of the Ruby culture. It allows programmers and software development teams to describe feature based tests in plain text definitions known as "stories". This text is written in a business readable domain-specific language(DSL) known as Gherkin.

Though Cucumber is a testing tool, the main intent of its development is to support Behavior Driven Development(BDD). This implies that the tests written in DSL are typically written before any other part of the code is written. It is analysed by the non technical stakeholders and then the production code is written. The production code is thus written outside-in, in such a way that the tests pass.

Cucumber vs Previous Stories based Testing tools

Cucumber is actually Aslak Hellesoy's revamp of RSpec's "Story Runner"(by Dan North). The earlier versions of "Stroy Runner" and its predecessor RBehave required that the stories be written in Ruby. But seeing the associated inconvenience, David Chelimsky added the plain text support with contributions done from an eclectic support team.

In April 2008, Aslak Hellesoy started the Cucumber project to iron out the inherent flaws and glictches of the RSpec Story Runner. Joseph Wilk and Ben Mabey were the regular contributors to the project. Matt Wynne joined in September 2009. In October 2009, Mike Sassak and Gregory Hnatiuk did some great work on a faster parser for Cucumber. In addition to the core team, there were over 250 developers contirbuting to the overall development of the project.

Example

# Language: English
Feature: InterestCalc
In order to avoid an error in interest calculation
As a new student
I want to be told the interest for given principal per year
 Scenario: Interest on Principle
  * I have entered 1000 into the interest_calc
  * I have entered  3.5 into the interest_calc
  * I press calc
  * The result should 35 on the screen

Step Definitions:

   begin require 'rpec/expectations';
   rescue LoadError;
   require 'spec/expectations';
   end
   require 'cucumber/formatter/unicode'
   $:.unshift(File.dirname(_FILE_) + '/../../lib')
   require 'Interest_calc'
   Given /I have entered (.*) into the interest_calc/ do |n|
     @calci=Interest_calc.new
     @calci.setdata(n)
   end
   When /I press (\w+)/ do |op|
     @result=@calci.calc
   end
   Then /the result should be (.*) on the screen/ do |result|
     @result.should == result.to_f
   end

Class def:

  class Interest_calc
    def push(n)
      @args ||= []
      @args<<n
    end
    def calc
      @args.inject(0){|p,r| p=p*r/100}
    end
  end