Module: RSpec::Given::ClassExtensions

Defined in:
lib/rspec/given/extensions.rb

Instance Method Summary collapse

Instance Method Details

#_rg_and_blocksObject



74
75
76
# File 'lib/rspec/given/extensions.rb', line 74

def _rg_and_blocks
  @_rg_and_blocks ||= []
end

#_rg_context_infoObject



78
79
80
# File 'lib/rspec/given/extensions.rb', line 78

def _rg_context_info
  @_rg_contet_info ||= {}
end

#_rg_givensObject

List of all givens directly in the current describe/context block.



64
65
66
# File 'lib/rspec/given/extensions.rb', line 64

def _rg_givens            # :nodoc:
  @_rg_givens ||= []
end

#_rg_invariantsObject

List of all invariants directly in the current describe/context block.



70
71
72
# File 'lib/rspec/given/extensions.rb', line 70

def _rg_invariants        # :nodoc:
  @_rg_invariants ||= []
end

#_rg_linesObject



82
83
84
# File 'lib/rspec/given/extensions.rb', line 82

def _rg_lines
  @_rg_lines ||= LineExtractor.new
end

#_rg_trigger_given(name) ⇒ Object

Trigger the evaluation of a Given! block by referencing its name.



88
89
90
# File 'lib/rspec/given/extensions.rb', line 88

def _rg_trigger_given(name) # :nodoc:
  Proc.new { send(name) }
end

#And(&block) ⇒ Object



193
194
195
196
# File 'lib/rspec/given/extensions.rb', line 193

def And(&block)
  fail "And defined without a Then" unless _rg_context_info[:then_defined]
  _rg_and_blocks << block
end

#Given(*args, &block) ⇒ Object

Declare a “given” of the current specification. If the given is named, the block will be lazily evaluated the first time the given is mentioned by name in the specification. If the given is unnamed, the block is evaluated for side effects every time the specification is executed.

:call-seq:

Given(:name, &block)
Given(&block)


125
126
127
128
129
130
131
# File 'lib/rspec/given/extensions.rb', line 125

def Given(*args, &block)
  if args.first.is_a?(Symbol)
    let(args.first, &block)
  else
    _rg_givens << block
  end
end

#Given!(name, &block) ⇒ Object

Declare a named given of the current specification. Similar to the named version of the “Given” command, except that the block is always evaluated.

:call-seq:

Given!(:name) { ... code ... }


139
140
141
142
# File 'lib/rspec/given/extensions.rb', line 139

def Given!(name, &block)
  let!(name, &block)
  _rg_givens << _rg_trigger_given(name)
end

#Invariant(&block) ⇒ Object

Establish an invariant that must be true for all Then blocks in the current (and nested) scopes.



189
190
191
# File 'lib/rspec/given/extensions.rb', line 189

def Invariant(&block)
  _rg_invariants << block
end

#Scenario(description, &block) ⇒ Object

DEPRECATED:

The Scenario command is deprecated. Using Scenario in a spec will result in a warning message. Eventually the command will be removed.

Declare a scenario to contain Given/When/Then declarations. A Scenario is essentially an RSpec context, with the additional expectations:

  • There is a single When declaration in a Scenario.

  • Scenarios do not nest.

:call-seq:

Scenario "a scenario description" do ... end


108
109
110
111
112
113
# File 'lib/rspec/given/extensions.rb', line 108

def Scenario(description, &block)
  line = eval("__LINE__", block.binding)
  file = eval("__FILE__", block.binding)
  puts "WARNING: Scenario is deprecated, please use either describe or context (#{file}:#{line})"
  context(description, &block)
end

#Then(&block) ⇒ Object

Provide an assertion about the specification.

Then supplies an assertion that should be true after all the Given and When blocks have been run. All invariants in scope will be checked before the Then block is run.



173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/rspec/given/extensions.rb', line 173

def Then(&block)
  b = block.binding
  file = eval "__FILE__", b
  line = eval "__LINE__", b
  description = _rg_lines.line(file, line) unless RSpec::Given.source_caching_disabled
  if description
    cmd = "it(description)"
  else
    cmd = "specify"
  end
  eval %{#{cmd} do _rg_then(&block) end}, binding, file, line
  _rg_context_info[:then_defined] = true
end

#When(*args, &block) ⇒ Object

Declare the code that is under test.

:call-seq:

When(:named_result, &block)
When(&block)


150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/rspec/given/extensions.rb', line 150

def When(*args, &block)
  if args.first.is_a?(Symbol)
    let!(args.first) do
      begin
        _rg_establish_givens
        instance_eval(&block)
      rescue Exception => ex
        Failure.new(ex)
      end
    end
  else
    before do
      _rg_establish_givens
      instance_eval(&block)
    end
  end
end