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(error_or_message = Exception, message = nil) ⇒ RaiseError

Returns a new instance of RaiseError.



5
6
7
8
9
10
11
12
13
# File 'lib/spec/matchers/raise_error.rb', line 5

def initialize(error_or_message=Exception, message=nil)
  if String === error_or_message
    @expected_error = Exception
    @expected_message = error_or_message
  else
    @expected_error = error_or_message
    @expected_message = message
  end
end

Instance Method Details

#descriptionObject



54
55
56
# File 'lib/spec/matchers/raise_error.rb', line 54

def description
  "raise #{expected_error}"
end

#failure_messageObject



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

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

#matches?(proc) ⇒ Boolean

Returns:

  • (Boolean)


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

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 @expected_message == @actual_error.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



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

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