Module: Spec::Story::World

Includes:
Example::Pending, Matchers
Defined in:
lib/gems/rspec-1.1.11/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.

Instance Attribute Summary

Attributes included from Matchers::ModuleMethods

#last_matcher, #last_should

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Matchers

#be, #be_close, #change, #eql, #equal, #exception_from, #exist, #fail, #fail_with, #have, #have_at_least, #have_at_most, #include, #map_specs, #match, #method_missing, #raise_error, #respond_to, #run_with, #satisfy, #simple_matcher, #smart_match, #throw_symbol

Methods included from Matchers::ModuleMethods

#clear_generated_description, #generated_description

Methods included from Example::Pending

#pending

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Spec::Matchers

Class Method Details

.add_listener(listener) ⇒ Object



30
31
32
# File 'lib/gems/rspec-1.1.11/lib/spec/story/world.rb', line 30

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

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



22
23
24
# File 'lib/gems/rspec-1.1.11/lib/spec/story/world.rb', line 22

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

.dry_runObject



90
91
92
# File 'lib/gems/rspec-1.1.11/lib/spec/story/world.rb', line 90

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

.errorsObject



86
87
88
# File 'lib/gems/rspec-1.1.11/lib/spec/story/world.rb', line 86

def errors
  @errors ||= []
end

.listenersObject



26
27
28
# File 'lib/gems/rspec-1.1.11/lib/spec/story/world.rb', line 26

def listeners
  @listeners ||= []
end

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



46
47
48
49
50
51
52
53
54
55
# File 'lib/gems/rspec-1.1.11/lib/spec/story/world.rb', line 46

def 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



34
35
36
# File 'lib/gems/rspec-1.1.11/lib/spec/story/world.rb', line 34

def step_mother
  @step_mother ||= StepMother.new
end

.step_namesObject



42
43
44
# File 'lib/gems/rspec-1.1.11/lib/spec/story/world.rb', line 42

def step_names
  @step_names ||= []
end

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



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

def 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



38
39
40
# File 'lib/gems/rspec-1.1.11/lib/spec/story/world.rb', line 38

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

Instance Method Details

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



123
124
125
# File 'lib/gems/rspec-1.1.11/lib/spec/story/world.rb', line 123

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

#errorsObject



99
100
101
# File 'lib/gems/rspec-1.1.11/lib/spec/story/world.rb', line 99

def errors
  World.errors
end

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



108
109
110
111
# File 'lib/gems/rspec-1.1.11/lib/spec/story/world.rb', line 108

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

#GivenScenario(name) ⇒ Object



103
104
105
106
# File 'lib/gems/rspec-1.1.11/lib/spec/story/world.rb', line 103

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

#start_collecting_errorsObject

end of class << self



95
96
97
# File 'lib/gems/rspec-1.1.11/lib/spec/story/world.rb', line 95

def start_collecting_errors
  errors.clear
end

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



118
119
120
121
# File 'lib/gems/rspec-1.1.11/lib/spec/story/world.rb', line 118

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

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



113
114
115
116
# File 'lib/gems/rspec-1.1.11/lib/spec/story/world.rb', line 113

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