Module: RSpec::Given::ClassExtensions

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

Instance Method Summary collapse

Instance Method Details

#_rg_givensObject

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



46
47
48
# File 'lib/rspec/given/extensions.rb', line 46

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

#_rg_invariantsObject

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



52
53
54
# File 'lib/rspec/given/extensions.rb', line 52

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

#_rg_trigger_given(name) ⇒ Object

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



58
59
60
# File 'lib/rspec/given/extensions.rb', line 58

def _rg_trigger_given(name) # :nodoc:
  Proc.new { send(name) }
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)


95
96
97
98
99
100
101
# File 'lib/rspec/given/extensions.rb', line 95

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 ... }


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

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.



152
153
154
# File 'lib/rspec/given/extensions.rb', line 152

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


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

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.



143
144
145
146
147
148
# File 'lib/rspec/given/extensions.rb', line 143

def Then(&block)
  b = block.binding
  file = eval "__FILE__", b
  line = eval "__LINE__", b
  eval %{specify do _rg_then(&block) end}, binding, file, line
end

#When(*args, &block) ⇒ Object

Declare the code that is under test.

:call-seq:

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


120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/rspec/given/extensions.rb', line 120

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