Module: Spec::Story::World

Includes:
Example::Pending, Matchers
Defined in:
lib/spec/story/world.rb

Overview

A World represents the actual instance a scenario will run in.

The runner ensures any instance variables and methods defined anywhere
in a story block are available to all the scenarios. This includes
variables that are created or referenced inside Given, When and Then
blocks.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.add_listener(listener) ⇒ Object



27
28
29
# File 'lib/spec/story/world.rb', line 27

def self.add_listener(listener)
  listeners() << listener
end

.create(cls = Object, *args) ⇒ Object



19
20
21
# File 'lib/spec/story/world.rb', line 19

def self.create(cls = Object, *args)
  cls.new(*args).extend(World)
end

.dry_runObject



87
88
89
# File 'lib/spec/story/world.rb', line 87

def self.dry_run
  ::Spec::Story::Runner.dry_run
end

.errorsObject



83
84
85
# File 'lib/spec/story/world.rb', line 83

def self.errors
  @errors ||= []
end

.listenersObject



23
24
25
# File 'lib/spec/story/world.rb', line 23

def self.listeners
  @listeners ||= []
end

.run_given_scenario_with_suspended_listeners(world, type, name, scenario) ⇒ Object



43
44
45
46
47
48
49
50
51
52
# File 'lib/spec/story/world.rb', line 43

def self.run_given_scenario_with_suspended_listeners(world, type, name, scenario)
  current_listeners = Array.new(listeners)
  begin
    listeners.each { |l| l.found_scenario(type, name) }
    @listeners.clear
    scenario.perform(world, name) unless dry_run
  ensure
    @listeners.replace(current_listeners)
  end
end

.step_motherObject



31
32
33
# File 'lib/spec/story/world.rb', line 31

def self.step_mother
  @step_mother ||= StepMother.new
end

.step_namesObject



39
40
41
# File 'lib/spec/story/world.rb', line 39

def self.step_names
  @step_names ||= []
end

.store_and_call(world, type, name, *args, &block) ⇒ Object



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
# File 'lib/spec/story/world.rb', line 54

def self.store_and_call(world, type, name, *args, &block)
  if block_given?
    step_mother.store(type, Step.new(name, &block))
  end
  step = step_mother.find(type, name)

  step_name = step.name
  step_names << step_name
  
  # It's important to have access to the parsed args here, so
  # we can give them to the listeners. The HTML reporter needs
  # the args so it can style them. See the generated output in
  # story_server/prototype/rspec_stories.html (generated by rake stories)
  args = step.parse_args(name) if args.empty?
  begin
    listeners.each { |l| l.step_upcoming(type, step_name, *args) }
    step.perform(world, *args) unless dry_run
    listeners.each { |l| l.step_succeeded(type, step_name, *args) }
  rescue Exception => e
    case e
    when Spec::Example::ExamplePendingError
      @listeners.each { |l| l.step_pending(type, step_name, *args) }
    else
      @listeners.each { |l| l.step_failed(type, step_name, *args) }
    end
    errors << e
  end
end

.use(steps) ⇒ Object



35
36
37
# File 'lib/spec/story/world.rb', line 35

def self.use(steps)
  step_mother.use(steps)
end

Instance Method Details

#And(name, *args, &block) ⇒ Object



119
120
121
# File 'lib/spec/story/world.rb', line 119

def And(name, *args, &block)
  World.store_and_call self, @__previous_step, name, *args, &block
end

#errorsObject



95
96
97
# File 'lib/spec/story/world.rb', line 95

def errors
  World.errors
end

#Given(name, *args, &block) ⇒ Object



104
105
106
107
# File 'lib/spec/story/world.rb', line 104

def Given(name, *args, &block)
  World.store_and_call self, :given, name, *args, &block
  @__previous_step = :given
end

#GivenScenario(name) ⇒ Object



99
100
101
102
# File 'lib/spec/story/world.rb', line 99

def GivenScenario(name)
  World.run_given_scenario_with_suspended_listeners(self, :'given scenario', name, GivenScenario.new(name))
  @__previous_step = :given
end

#start_collecting_errorsObject



91
92
93
# File 'lib/spec/story/world.rb', line 91

def start_collecting_errors
  errors.clear
end

#Then(name, *args, &block) ⇒ Object



114
115
116
117
# File 'lib/spec/story/world.rb', line 114

def Then(name, *args, &block)
  World.store_and_call self, :then, name, *args, &block
  @__previous_step = :then
end

#When(name, *args, &block) ⇒ Object



109
110
111
112
# File 'lib/spec/story/world.rb', line 109

def When(name, *args, &block)
  World.store_and_call self, :when, name, *args, &block
  @__previous_step = :when
end