Module: RSpec::Given::Extensions

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

Instance Method Summary collapse

Instance Method Details

#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)


37
38
39
40
41
42
43
# File 'lib/rspec/given/extensions.rb', line 37

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


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

def Given!(name, &block)
  let!(name, &block)
end

#Scenario(description, &block) ⇒ Object

DEPRECATED:

The Scenario command is deprecated. Future versions of rspec/given will start displaying warnings when used. 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


23
24
25
# File 'lib/rspec/given/extensions.rb', line 23

def Scenario(description, &block)
  context(description, &block)
end

#When(*args, &block) ⇒ Object

Declare the code that is under test.

:call-seq:

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


61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/rspec/given/extensions.rb', line 61

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