changelog shortlog tags changeset manifest revisions annotate raw

vendor/plugins/rspec/spec/spec/story/runner/story_runner_spec.rb

changeset 15: 64acf98d15f4
author: moriq@moriq.com
date: Mon Mar 10 10:12:58 2008 +0900 (16 years ago)
permissions: -rw-r--r--
description: add plugins rspec
1require File.dirname(__FILE__) + '/../story_helper'
2
3module Spec
4 module Story
5 module Runner
6 describe StoryRunner do
7 it 'should collect all the stories' do
8 # given
9 story_runner = StoryRunner.new(stub('scenario_runner'))
10
11 # when
12 story_runner.Story 'title1', 'narrative1' do end
13 story_runner.Story 'title2', 'narrative2' do end
14 stories = story_runner.stories
15
16 # then
17 story_runner.should have(2).stories
18 stories.first.title.should == 'title1'
19 stories.first.narrative.should == 'narrative1'
20 stories.last.title.should == 'title2'
21 stories.last.narrative.should == 'narrative2'
22 end
23
24 it 'should gather all the scenarios in the stories' do
25 # given
26 story_runner = StoryRunner.new(stub('scenario_runner'))
27
28 # when
29 story_runner.Story "story1", "narrative1" do
30 Scenario "scenario1" do end
31 Scenario "scenario2" do end
32 end
33 story_runner.Story "story2", "narrative2" do
34 Scenario "scenario3" do end
35 end
36 scenarios = story_runner.scenarios
37
38 # then
39 story_runner.should have(3).scenarios
40 scenarios[0].name.should == 'scenario1'
41 scenarios[1].name.should == 'scenario2'
42 scenarios[2].name.should == 'scenario3'
43 end
44
45 # captures worlds passed into a ScenarioRunner
46 class ScenarioWorldCatcher
47 attr_accessor :worlds
48 def run(scenario, world)
49 (@worlds ||= []) << world
50 end
51 end
52
53 it 'should run each scenario in a separate object' do
54 # given
55 scenario_world_catcher = ScenarioWorldCatcher.new
56 story_runner = StoryRunner.new(scenario_world_catcher)
57 story_runner.Story 'story', 'narrative' do
58 Scenario 'scenario1' do end
59 Scenario 'scenario2' do end
60 end
61
62 # when
63 story_runner.run_stories
64
65 # then
66 worlds = scenario_world_catcher.worlds
67 scenario_world_catcher.should have(2).worlds
68 worlds[0].should_not == worlds[1]
69 end
70
71 it 'should use the provided world creator to create worlds' do
72 # given
73 stub_scenario_runner = stub_everything
74 mock_world_creator = mock('world creator')
75 story_runner = StoryRunner.new(stub_scenario_runner, mock_world_creator)
76 story_runner.Story 'story', 'narrative' do
77 Scenario 'scenario1' do end
78 Scenario 'scenario2' do end
79 end
80
81 # expect
82 mock_world_creator.should_receive(:create).twice
83
84 # when
85 story_runner.run_stories
86
87 # then
88 end
89
90 it 'should notify listeners of the scenario count when the run starts' do
91 # given
92 story_runner = StoryRunner.new(stub_everything)
93 mock_listener1 = stub_everything('listener1')
94 mock_listener2 = stub_everything('listener2')
95 story_runner.add_listener(mock_listener1)
96 story_runner.add_listener(mock_listener2)
97
98 story_runner.Story 'story1', 'narrative1' do
99 Scenario 'scenario1' do end
100 end
101 story_runner.Story 'story2', 'narrative2' do
102 Scenario 'scenario2' do end
103 Scenario 'scenario3' do end
104 end
105
106 # expect
107 mock_listener1.should_receive(:run_started).with(3)
108 mock_listener2.should_receive(:run_started).with(3)
109
110 # when
111 story_runner.run_stories
112
113 # then
114 end
115
116 it 'should notify listeners when a story starts' do
117 # given
118 story_runner = StoryRunner.new(stub_everything)
119 mock_listener1 = stub_everything('listener1')
120 mock_listener2 = stub_everything('listener2')
121 story_runner.add_listener(mock_listener1)
122 story_runner.add_listener(mock_listener2)
123
124 story_runner.Story 'story1', 'narrative1' do
125 Scenario 'scenario1' do end
126 end
127 story_runner.Story 'story2', 'narrative2' do
128 Scenario 'scenario2' do end
129 Scenario 'scenario3' do end
130 end
131
132 # expect
133 mock_listener1.should_receive(:story_started).with('story1', 'narrative1')
134 mock_listener1.should_receive(:story_ended).with('story1', 'narrative1')
135 mock_listener2.should_receive(:story_started).with('story2', 'narrative2')
136 mock_listener2.should_receive(:story_ended).with('story2', 'narrative2')
137
138 # when
139 story_runner.run_stories
140
141 # then
142 end
143
144 it 'should notify listeners when the run ends' do
145 # given
146 story_runner = StoryRunner.new(stub_everything)
147 mock_listener1 = stub_everything('listener1')
148 mock_listener2 = stub_everything('listener2')
149 story_runner.add_listener mock_listener1
150 story_runner.add_listener mock_listener2
151 story_runner.Story 'story1', 'narrative1' do
152 Scenario 'scenario1' do end
153 end
154
155 # expect
156 mock_listener1.should_receive(:run_ended)
157 mock_listener2.should_receive(:run_ended)
158
159 # when
160 story_runner.run_stories
161
162 # then
163 end
164
165 it 'should run a story in an instance of a specified class' do
166 # given
167 scenario_world_catcher = ScenarioWorldCatcher.new
168 story_runner = StoryRunner.new(scenario_world_catcher)
169 story_runner.Story 'title', 'narrative', :type => String do
170 Scenario 'scenario' do end
171 end
172
173 # when
174 story_runner.run_stories
175
176 # then
177 scenario_world_catcher.worlds[0].should be_kind_of(String)
178 scenario_world_catcher.worlds[0].should be_kind_of(World)
179 end
180
181 it 'should pass initialization params through to the constructed instance' do
182 # given
183 scenario_world_catcher = ScenarioWorldCatcher.new
184 story_runner = StoryRunner.new(scenario_world_catcher)
185 story_runner.Story 'title', 'narrative', :type => Array, :args => [3] do
186 Scenario 'scenario' do end
187 end
188
189 # when
190 story_runner.run_stories
191
192 # then
193 scenario_world_catcher.worlds[0].should be_kind_of(Array)
194 scenario_world_catcher.worlds[0].size.should == 3
195 end
196
197 it 'should find a scenario in the current story by name' do
198 # given
199 story_runner = StoryRunner.new(ScenarioRunner.new)
200 $scenario = nil
201
202 story_runner.Story 'title', 'narrative' do
203 Scenario 'first scenario' do
204 end
205 Scenario 'second scenario' do
206 $scenario = StoryRunner.scenario_from_current_story 'first scenario'
207 end
208 end
209
210 # when
211 story_runner.run_stories
212
213 # then
214 $scenario.name.should == 'first scenario'
215 end
216
217 it "should clean the steps between stories" do
218 #given
219 story_runner = StoryRunner.new(ScenarioRunner.new)
220 result = mock 'result'
221
222 step1 = Step.new('step') do
223 result.one
224 end
225 steps1 = StepGroup.new
226 steps1.add :when, step1
227
228 story_runner.Story 'title', 'narrative', :steps => steps1 do
229 Scenario 'first scenario' do
230 When 'step'
231 end
232 end
233
234 step2 = Step.new('step') do
235 result.two
236 end
237 steps2 = StepGroup.new
238 steps2.add :when, step2
239
240 story_runner.Story 'title2', 'narrative', :steps => steps2 do
241 Scenario 'second scenario' do
242 When 'step'
243 end
244 end
245
246 #then
247 result.should_receive(:one)
248 result.should_receive(:two)
249
250 #when
251 story_runner.run_stories
252 end
253 end
254 end
255 end
256end