Class: MotionSpec::Matcher::RaiseError

Inherits:
Object
  • Object
show all
Defined in:
lib/motion-spec/matcher/raise_error.rb

Instance Method Summary collapse

Constructor Details

#initialize(error_class = Exception, message = '') ⇒ RaiseError

Returns a new instance of RaiseError.



5
6
7
8
# File 'lib/motion-spec/matcher/raise_error.rb', line 5

def initialize(error_class = Exception, message = '')
  @error_class = error_class.is_a?(Class) ? error_class : Exception
  @error_message = (error_class.is_a?(String) || error_class.is_a?(Regexp)) ? error_class : message
end

Instance Method Details

#exception_matches(exception) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/motion-spec/matcher/raise_error.rb', line 18

def exception_matches(exception)
  return false unless exception.is_a?(@error_class)

  is_match = case @error_message
              when String
                exception.message.include?(@error_message)
              when Regexp
                @error_message.match(exception.message)
              else
                false
              end

  return false unless is_match

  true
end

#fail!(subject, negated) ⇒ Object

Raises:



35
36
37
38
39
40
41
42
43
44
# File 'lib/motion-spec/matcher/raise_error.rb', line 35

def fail!(subject, negated)
  show_class = @error_class != Exception
  show_message = !@error_message.is_a?(String) || !@error_message.empty?
  raise FailedExpectation.new(
    FailMessageRenderer.message_for_raise_error(
      negated, show_class, @error_class, show_message, @error_message,
      @rescued_exception
    )
  )
end

#matches?(value, &block) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
13
14
15
16
# File 'lib/motion-spec/matcher/raise_error.rb', line 10

def matches?(value, &block)
  block.call
  false
rescue Exception => e
  @rescued_exception = e
  exception_matches(e)
end