Module: RSpec::Undefined::DSL

Defined in:
lib/rspec/undefined/dsl.rb

Instance Method Summary collapse

Instance Method Details

#undefined(description, category: nil, &block) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rspec/undefined/dsl.rb', line 9

def undefined(description, category: nil, &block)
  unless category.nil? || category.is_a?(Symbol)
    raise ArgumentError,
          "category は Symbol で指定してください(受け取った値: #{category.inspect})。" \
          "カスタムカテゴリは RSpec::Undefined::Categories.register で事前登録してください。"
  end
  loc = caller_locations(1, 1).first
  location = loc ? "#{loc.path}:#{loc.lineno}" : nil
  example("[undefined] #{description}", undefined: true, undefined_category: category) do
    RSpec::Undefined.registry.add(
      RSpec::Undefined::Entry.new(
        kind: :declaration,
        description: description,
        category: category,
        location: location,
        example_id: RSpec.current_example && RSpec.current_example.id
      )
    )

    if RSpec::Undefined.configuration.strict?
      raise RSpec::Expectations::ExpectationNotMetError,
            "undefined declaration (strict mode): #{description}"
    end

    if block
      begin
        instance_exec(&block)
      rescue RSpec::Expectations::ExpectationNotMetError
        # 通常モードではブロック内 failure を握り潰す
      end
    end
  end
end