Sha256: 6721421500e75e8c16546154f30f218e9d85553cb69df347af2743059b3610da

Contents?: true

Size: 1.88 KB

Versions: 44

Compression:

Stored size: 1.88 KB

Contents

Feature: Transforms

  If you see certain phrases repeated over and over in your step definitions, you can
  use transforms to factor out that duplication, and make your step definitions simpler.
  
  Background:
    Let's just create a simple feature for testing out Transforms.
    We also have a Person class that we need to be able to build.
    
    Given a file named "features/foo.feature" with:
      """
      Feature:
        Scenario:
          Given a Person aged 15 with blonde hair
      """
    And a file named "features/support/person.rb" with:
      """
      class Person < Struct.new(:age)
        def to_s
          "I am #{age} years old"
        end
      end
      """

  Scenario: Basic Transform
    This is the most basic way to use a transform. Notice that the regular
    expression is pretty much duplicated.
    
    And a file named "features/step_definitions/steps.rb" with:
      """
      Transform(/a Person aged (\d+)/) do |age|
        Person.new(age.to_i)
      end
      
      Given /^(a Person aged \d+) with blonde hair$/ do |person|
        puts "#{person} and I have blonde hair"
      end
      """
    When I run cucumber "features/foo.feature"
    Then it should pass with:
      """
      I am 15 years old and I have blonde hair
      """
  
  Scenario: Re-use Transform's Regular Expression
    If you keep a reference to the transform, you can use it in your
    regular expressions to avoid repeating the regular expression.

    And a file named "features/step_definitions/steps.rb" with:
      """
      A_PERSON = Transform(/a Person aged (\d+)/) do |age|
        Person.new(age.to_i)
      end

      Given /^(#{A_PERSON}) with blonde hair$/ do |person|
        puts "#{person} and I have blonde hair"
      end
      """
    When I run cucumber "features/foo.feature"
    Then it should pass with:
      """
      I am 15 years old and I have blonde hair
      """

Version data entries

44 entries across 42 versions & 9 rubygems

Version Path
candlepin-api-0.4.0 bundle/ruby/gems/cucumber-1.2.1/features/transforms.feature
candlepin-api-0.4.0 bundle/ruby/1.8/gems/cucumber-1.2.1/features/transforms.feature
candlepin-api-0.4.0 bundle/ruby/1.9.1/gems/cucumber-1.2.1/features/transforms.feature
librarian-puppet-0.9.9 vendor/gems/ruby/1.9.1/gems/cucumber-1.2.1/features/transforms.feature
cucumber-1.2.5 features/transforms.feature
cucumber-1.2.3 features/transforms.feature
cucumber-1.2.2 features/transforms.feature
librarian-puppet-0.9.8 vendor/gems/ruby/1.9.1/gems/cucumber-1.2.1/features/transforms.feature
librarian-puppet-0.9.4 vendor/gems/ruby/1.8/gems/cucumber-1.2.1/features/transforms.feature
casecumber-1.0.2.1 features/transforms.feature
casecumber-1.2.1.cb2 features/transforms.feature
librarian-puppet-0.9.3 vendor/gems/ruby/1.8/gems/cucumber-1.2.1/features/transforms.feature
cucumber-1.2.1 features/transforms.feature
resque-pool-0.3.0 vendor/bundle/ruby/1.8/gems/cucumber-1.1.9/features/transforms.feature
ftl-0.2.0 vendor/bundle/gems/cucumber-1.2.0/features/transforms.feature
cucumber-1.2.0 features/transforms.feature
frameworks-capybara-0.2.0.rc6 vendor/bundle/ruby/1.8/gems/cucumber-1.1.9/features/transforms.feature
frameworks-capybara-0.2.0.rc5 vendor/bundle/ruby/1.8/gems/cucumber-1.1.9/features/transforms.feature
frameworks-capybara-0.2.0.rc4 vendor/bundle/ruby/1.8/gems/cucumber-1.1.9/features/transforms.feature
frameworks-capybara-0.2.0.rc3 vendor/bundle/ruby/1.8/gems/cucumber-1.1.9/features/transforms.feature