Module: RSpec::Given::ClassExtensions

Included in:
Spec::ExampleGroup
Defined in:
lib/rspec/given/extensions.rb

Instance Method Summary collapse

Instance Method Details

#_rgc_and_blocksObject



127
128
129
# File 'lib/rspec/given/extensions.rb', line 127

def _rgc_and_blocks
  @_rgc_and_blocks ||= []
end

#_rgc_context_infoObject



131
132
133
# File 'lib/rspec/given/extensions.rb', line 131

def _rgc_context_info
  @_rgc_context_info ||= {}
end

#_rgc_givensObject

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



117
118
119
# File 'lib/rspec/given/extensions.rb', line 117

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

#_rgc_invariantsObject

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



123
124
125
# File 'lib/rspec/given/extensions.rb', line 123

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

#_rgc_linesObject



135
136
137
# File 'lib/rspec/given/extensions.rb', line 135

def _rgc_lines
  @_rgc_lines ||= LineExtractor.new
end

#_rgc_trigger_given(name) ⇒ Object

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



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

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

#And(&block) ⇒ Object



251
252
253
254
# File 'lib/rspec/given/extensions.rb', line 251

def And(&block)
  fail "And defined without a Then" unless _rgc_context_info[:then_defined]
  _rgc_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) { ... code ... }
Given { ... code ... }


177
178
179
180
181
182
183
# File 'lib/rspec/given/extensions.rb', line 177

def Given(*args, &block)
  if args.first.is_a?(Symbol)
    let(args.first, &block)
  else
    _rgc_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 ... }


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

def Given!(name, &block)
  let(name, &block)
  _rgc_givens << _rgc_trigger_given(name)
end

#Invariant(&block) ⇒ Object

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



247
248
249
# File 'lib/rspec/given/extensions.rb', line 247

def Invariant(&block)
  _rgc_invariants << block
end

#Scenario(description, &block) ⇒ Object

DEPRECATED:

The Scenario command is deprecated. Using Scenario in an example 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


161
162
163
164
165
# File 'lib/rspec/given/extensions.rb', line 161

def Scenario(description, &block)
  file, line = eval("[__FILE__, __LINE__]", 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.

:call-seq:

Then { ... assertion ... }


232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/rspec/given/extensions.rb', line 232

def Then(&block)
  env = block.binding
  file, line = eval "[__FILE__, __LINE__]", env
  description = _rgc_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
  _rgc_context_info[:then_defined] = true
end

#use_natural_assertions(enabled = true) ⇒ Object



256
257
258
259
# File 'lib/rspec/given/extensions.rb', line 256

def use_natural_assertions(enabled=true)
  RSpec::Given.ok_to_use_natural_assertions(enabled)
  _rgc_context_info[:natural_assertions_enabled] = enabled
end

#When(*args, &block) ⇒ Object

Declare the code that is under test.

:call-seq:

When(:named_result) { ... code_under_test ... }
When { ... code_under_test ... }


203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/rspec/given/extensions.rb', line 203

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