Class: Matchy::Expectations::RaiseErrorExpectation

Inherits:
Base
  • Object
show all
Defined in:
lib/matchy/built_in/error_expectations.rb

Instance Method Summary collapse

Methods inherited from Base

#fail!, #pass!

Constructor Details

#initialize(expected, test_case) ⇒ RaiseErrorExpectation

Returns a new instance of RaiseErrorExpectation.



4
5
6
7
# File 'lib/matchy/built_in/error_expectations.rb', line 4

def initialize(expected, test_case)
  @error = nil
  super
end

Instance Method Details

#failure_messageObject



22
23
24
25
26
27
28
29
30
31
# File 'lib/matchy/built_in/error_expectations.rb', line 22

def failure_message
  extra = ""
  if @error
    extra = "but #{@error.class.name} was raised instead"
  else
    extra = "but none was raised"
  end
  
  "Expected #{@receiver.inspect} to raise #{@expected.name}, #{extra}."
end

#matches?(receiver) ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/matchy/built_in/error_expectations.rb', line 9

def matches?(receiver)
  @receiver = receiver
  begin
    receiver.call
    return false
  rescue StandardError => e
    @error = e
    return false unless e.class.ancestors.include?(@expected)
  
    return true
  end
end

#negative_failure_messageObject



33
34
35
# File 'lib/matchy/built_in/error_expectations.rb', line 33

def negative_failure_message
  "Expected #{@receiver.inspect} to not raise #{@expected.name}."
end