Thursday, January 17, 2013

irb Console for Cucumber and Working Example


Most folks run cucumber features in their entirety from the command line.

Have you ever wanted to run individual steps interactively?

I'll tell you how in this blog posting...

Install the crb


crb is an irb console for cucumber world.

gem install crb

crb Documentation

crb
===
  An irb console for cucumber world

Features
========
  * Can define steps interactively
  * Can execute steps interactively like debugger
  * Can share cuke world and irb context in a same object
  * Can see instance variables of step files via irb
  * Supported hooks (but only before/after without parameters)
  * Supported World methods


Run crb 
$ cd to_root_of_your_project
$ crb


irb(CRB:3):001:0> Given "I have entered 3"
Undefined step: "I have entered 3"
=> #<Cucumber::Undefined: Undefined step: "I have entered 3">

irb(CRB:3):002:0> Given "I have entered 3 into the calculator"
=> [3]

irb(CRB:3):005:0> Given "I have entered 5 into the calculator"
=> [3, 5]

irb(CRB:3):006:0> Then "I press add"
=> 8

irb(CRB:3):007:0> @calc
=> #<Calculator:0x007fa7da9d0768 @args=[3, 5]>

irb(CRB:3):008:0> @calc.push 1
=> [3, 5, 1]

irb(CRB:3):009:0> Then "I press add"
=> 9

irb(CRB:3):010:0> Then "I press divide"
=> 0.6



Project Files


features/addition.feature


Feature: Addition
  In order to avoid silly mistakes
  As a math idiot 
  I want to be told the sum of two numbers

  Scenario Outline: Add two numbers
    Given I have entered <input_1> into the calculator
    And I have entered <input_2> into the calculator
    When I press <button>
    Then the result should be <output> on the screen

  Examples:
    | input_1 | input_2 | button | output |
    | 20      | 30      | add    | 50     |
    | 2       | 5       | add    | 7      |
    | 0       | 40      | add    | 40     |



features/step_definitions/calculator_steps.rb




# encoding: utf-8
begin require 'rspec/expectations'; rescue LoadError; require 'spec/expectations'; end 
require 'cucumber/formatter/unicode'
$:.unshift(File.dirname(__FILE__) + '/../../lib')
require 'calculator'

Before do
  @calc = Calculator.new
end

After do
end

Given /I have entered (\d+) into the calculator/ do |n|
  @calc.push n.to_i
end

When /I press (\w+)/ do |op|
  @result = @calc.send op
end

Then /the result should be (.*) on the screen/ do |result|
  @result.should == result.to_f
end

lib/calculator.rb




class Calculator
  def push(n)
    @args ||= []
    @args << n
  end
  
  def add
    @args.inject(0){|n,sum| sum+=n}
  end

  def divide
    @args[0].to_f / @args[1].to_f
  end
end


Sponsor Ads


2 comments:

  1. readings were really very good. I will bookmark this blog so that I could often visit this blog often to read the latest article from your blog. I wait for your arrival at our website ...

    thanks, ...


    By : android developer indonesia | firzil.co.id

    ReplyDelete
  2. Dear Lex Sheehan,

    when i run crb, i have this error:

    /usr/local/share/gems/gems/crb-1.0.1/lib/crb.rb:97:in `load_step_definitions': undefined method `support_to_load' for # (NoMethodError)
    from /usr/local/share/gems/gems/crb-1.0.1/lib/crb.rb:103:in `execute!'
    from /home/antonio/.gem/ruby/1.9.1/gems/cucumber-2.4.0/lib/cucumber/cli/main.rb:11:in `execute'
    from /usr/local/share/gems/gems/crb-1.0.1/bin/crb:7:in `'
    from /usr/local/bin/crb:23:in `load'
    from /usr/local/bin/crb:23:in `'

    can you help me?

    ReplyDelete