Class: Spec::Matchers::RaiseError

Inherits:
Object
  • Object
show all
Defined in:
lib/spec/matchers/raise_error.rb

Overview

:nodoc:

Instance Method Summary collapse

Constructor Details

#initialize(exception = Exception, message = nil) ⇒ RaiseError

Returns a new instance of RaiseError.



5
6
7
8
# File 'lib/spec/matchers/raise_error.rb', line 5

def initialize(exception=Exception, message=nil)
  @expected_error = exception
  @expected_message = message
end

Instance Method Details

#descriptionObject



49
50
51
# File 'lib/spec/matchers/raise_error.rb', line 49

def description
  "raise #{expected_error}"
end

#failure_messageObject



41
42
43
# File 'lib/spec/matchers/raise_error.rb', line 41

def failure_message
  return "expected #{expected_error}#{actual_error}" if @raised_other || !@raised_expected_error
end

#matches?(proc) ⇒ Boolean

Returns:

  • (Boolean)


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
# File 'lib/spec/matchers/raise_error.rb', line 10

def matches?(proc)
  @raised_expected_error = false
  @raised_other = false
  begin
    proc.call
  rescue @expected_error => @actual_error
    if @expected_message.nil?
      @raised_expected_error = true
    else
      case @expected_message
      when Regexp
        if @expected_message =~ @actual_error.message
          @raised_expected_error = true
        else
          @raised_other = true
        end
      else
        if @actual_error.message == @expected_message
          @raised_expected_error = true
        else
          @raised_other = true
        end
      end
    end
  rescue => @actual_error
    @raised_other = true
  ensure
    return @raised_expected_error
  end
end

#negative_failure_messageObject



45
46
47
# File 'lib/spec/matchers/raise_error.rb', line 45

def negative_failure_message
  "expected no #{expected_error}#{actual_error}"
end