Skip to content

Commit

Permalink
Cleanup example for step reuse
Browse files Browse the repository at this point in the history
  • Loading branch information
leshill committed Nov 21, 2011
1 parent ab28cd7 commit a91e063
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 19 deletions.
43 changes: 33 additions & 10 deletions README.md
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -168,11 +168,13 @@ and


### Reusing steps ### Reusing steps
When using scoped steps in Turnip, you can tell it to also include steps When using scoped steps in Turnip, you can tell it to also include steps
defined in another `steps_for` block. The syntax for that is `uses_steps`: defined in another `steps_for` block. The syntax for that is `use_steps`:


``` ruby ``` ruby
# dragon_steps.rb # dragon_steps.rb
steps_for :dragon do steps_for :dragon do
use_steps :knight

attr_accessor :dragon attr_accessor :dragon


def dragon_attack def dragon_attack
Expand All @@ -183,8 +185,8 @@ steps_for :dragon do
self.dragon = 1 self.dragon = 1
end end


step "the dragon attacks for :count hitpoints" do |count| step "the dragon attacks the knight" do
dragon_attack.should eq(count) knight.attacked_for(dragon_attack)
end end
end end


Expand All @@ -197,23 +199,44 @@ steps_for :red_dragon do
def dragon_attack def dragon_attack
attack = super attack = super
if red_dragon if red_dragon
attack + 10 attack + 15
else else
attack attack
end end
end end


step "it is a fire breathing red dragon" do step "the dragon breathes fire" do
self.red_dragon = 1 self.red_dragon = 1
end end
end end
``` ```


Notice in this example we are making full use of Ruby's modules including
using super to call the included module's version of `dragon_attack`. In this example we are making full use of Ruby's modules including using super
to call the included module's version of `dragon_attack`, for example with the
following feature file:

``` cucumber
Feature: Red Dragons are deadly
@dragon
Scenario:
Given there is a dragon
And there is a knight
When the dragon attacks the knight
Then the knight is alive
@red_dragon
Scenario:
Given there is a dragon
And the dragon breathes fire
And there is a knight
When the dragon attacks the knight
Then the knight is dead
```


### Auto-included steps ### Auto-included steps
By default, Turnip will automatically make available any steps defined in By default, Turnip will automatically make available any steps defined in
a `steps_for` block with the same name as the feature file being run. For a `steps_for` block with the same name as the feature file being run. For
example, given this step file: example, given this step file:


Expand All @@ -223,11 +246,11 @@ steps_for :user_signup do
step "I am on the homepage" do step "I am on the homepage" do
... ...
end end

step "I signup with valid info" do step "I signup with valid info" do
... ...
end end

step "I should see a welcome message" do step "I should see a welcome message" do
end end
end end
Expand Down
6 changes: 4 additions & 2 deletions examples/dragon_steps.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,4 +1,6 @@
steps_for :dragon do steps_for :dragon do
use_steps :knight

attr_accessor :dragon attr_accessor :dragon


def dragon_attack def dragon_attack
Expand All @@ -9,7 +11,7 @@ def dragon_attack
self.dragon = 1 self.dragon = 1
end end


step "the dragon attacks for :count hitpoints" do |count| step "the dragon attacks the knight" do
dragon_attack.should eq(count) knight.attacked_for(dragon_attack)
end end
end end
29 changes: 29 additions & 0 deletions examples/knight_steps.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,29 @@
steps_for :knight do
attr_accessor :knight

class Knight
def initialize
@hp = 20
end

def alive?
@hp > 0
end

def attacked_for(amount)
@hp -= amount
end
end

step "there is a knight" do
self.knight = Knight.new
end

step "the knight is alive" do
knight.should be_alive
end

step "the knight is dead" do
knight.should_not be_alive
end
end
5 changes: 2 additions & 3 deletions examples/red_dragon_steps.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@
def dragon_attack def dragon_attack
attack = super attack = super
if red_dragon if red_dragon
attack + 10 attack + 15
else else
attack attack
end end
end end


step "it is a fire breathing red dragon" do step "the dragon breathes fire" do
self.red_dragon = 1 self.red_dragon = 1
end end
end end

13 changes: 9 additions & 4 deletions examples/steps_for_super.feature
Original file line number Original file line Diff line number Diff line change
@@ -1,11 +1,16 @@
Feature: Tagged steps for and super Feature: Red Dragons are deadly

@dragon @dragon
Scenario: Scenario:
Given there is a dragon Given there is a dragon
Then the dragon attacks for 10 hitpoints And there is a knight
When the dragon attacks the knight
Then the knight is alive


@red_dragon @red_dragon
Scenario: Scenario:
Given there is a dragon Given there is a dragon
And it is a fire breathing red dragon And the dragon breathes fire
Then the dragon attacks for 20 hitpoints And there is a knight
When the dragon attacks the knight
Then the knight is dead

0 comments on commit a91e063

Please sign in to comment.