Module: RSpec::Core::SharedExampleGroup

Defined in:
lib/rspec/core/shared_example_group.rb

Instance Method Summary collapse

Instance Method Details

#share_as(name, &block) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/rspec/core/shared_example_group.rb', line 32

def share_as(name, &block)
  if Object.const_defined?(name)
    mod = Object.const_get(name)
    raise_name_error unless mod.created_from_caller(caller)
  end

  mod = Module.new do
    @shared_block = block
    @caller_line = caller.last

    def self.created_from_caller(other_caller)
      @caller_line == other_caller.last
    end

    def self.included(kls)
      kls.describe(&@shared_block)
      kls.children.first.[:shared_group_name] = name
    end
  end

  shared_const = Object.const_set(name, mod)
  RSpec.world.shared_example_groups[shared_const] = block
end

#shared_examples(name, &block) ⇒ Object #shared_examples(name, tags, &block) ⇒ Object Also known as: shared_context, share_examples_for, shared_examples_for

Creates and stores (but does not evaluate) the block.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/rspec/core/shared_example_group.rb', line 12

def shared_examples(*args, &block)
  if [String, Symbol, Module].any? {|cls| cls === args.first }
    object = args.shift
    ensure_shared_example_group_name_not_taken(object)
    RSpec.world.shared_example_groups[object] = block
  end

  unless args.empty?
    mod = Module.new
    (class << mod; self; end).send(:define_method, :extended) do |host|
      host.class_eval(&block)
    end
    RSpec.configuration.extend(mod, *args)
  end
end