public
Description: Spec the steps in rspec stories.
Homepage: http://workunitgroup.com/2008/4/23/crafting-rspec-steps-with-step_eval-and-drying-them-with-a-helper
Clone URL: git://github.com/mhennemeyer/stepspecr.git
stepspecr / README
100644 125 lines (90 sloc) 3.164 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
= StepSpecr
 
by Matthias Hennemeyer <mhennemeyer@gmail.com>
 
== Introduction
 
StepSpecr is a Rails plugin intended to be used with Rspec User Stories.
It provides a 'testing' framework for speccing Given/When/Then steps within Rspec examples. This lets you implement GWT-steps the BDD way.
 
 
== Examples
 
=== A rather trivial one
 
In a plaintext story the following step is needed:
  Given 1 articles
Instead of 'just implement' and 'running the story as a test': write a spec:
 
in PROJECT_HOME/spec/steps/article_steps_spec.rb
 
  require File.expand_path(File.dirname(__FILE__) + "/stepspecr_helper.rb")
  
  describe "Given $count articles" do
    it "should create 1 articles for count=1" do
      class Article
      end
      Article.should_receive :create
      step_eval "Given 1 article", :articles
    end
  end
    
Running the example will perform the following actions:
  
1. It will (try to) collect the steps supplied to step_group :articles
2. It will run the step
3. It will FAIL ...
 
  $ script/spec --format specdoc spec/steps/article_steps_spec.rb
 
Implement the step:
  
in PROJECT_HOME/stories/steps/article_steps.rb
 
  steps_for :articles do
    Given "$count articles" do |count|
      count.to_i.times { Article.create }
    end
  end
  
 
Run the example again and it should PASS.
 
=== A more complex example
 
You want a step that creates modelobjects that are specified in the story:
  Given 5 articles
  Given 1 post
  Given 17 lists
  ...
 
Using StepSpecr.step is a little bit more involved than plain step_eval but gives you
a facility to configure the thing so that you can DRY it up.
This will be more than one example: (Just showing the description - BDD doesn't mean to write more than one example at a time)
 
   describe "Given $count $models" do
     before(:all) do
       StepSpecr.configure do
         step_group :resources
       end
     end
     
     it "should create the specified model" do
       StepSpecr.spec "Given 1 specific_model" do
         before do
           class SpecificModel
           end
           SpecificModel.should_receive(:create)
         end
       end
     end
     
     it "should create the specified number of models" do
        StepSpecr.spec "Given 5 specific_models" do
          before do
            class SpecificModel
            end
            SpecificModel.should_receive(:create).exactly(5).times
          end
        end
      end
     
     it "should create 17 lists for count=17, models=lists" do
       StepSpecr.spec "Given 17 lists" do
         before do
           class List
           end
           List.should_receive(:create).exactly(17).times
         end
       end
     end
   end
   
The implementation could look like this:
 
   steps_for :resources do
     Given "$count $models" do |count, name|
       klass = eval "#{name.singularize.camelize}"
       count.to_i.times { klass.create }
     end
   end
 
== REQUIREMENTS:
 
* Rspec >= 1.1.3
* rspec_on_rails
 
 
== INSTALL:
 
  $ ruby script/plugin install git://github.com/mhennemeyer/stepspecr.git
  $ script/generate stepspecr
  
 
Copyright (c) 2008 Matthias Hennemeyer, released under the MIT license