Class: RSpec::Matchers::BuiltIn::RaiseError

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

Instance Method Summary collapse

Constructor Details

#initialize(expected_error_or_message = Exception, expected_message = nil, &block) ⇒ RaiseError

Returns a new instance of RaiseError.



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

def initialize(expected_error_or_message=Exception, expected_message=nil, &block)
  @block = block
  @actual_error = nil
  case expected_error_or_message
  when String, Regexp
    @expected_error, @expected_message = Exception, expected_error_or_message
  else
    @expected_error, @expected_message = expected_error_or_message, expected_message
  end
end

Instance Method Details

#descriptionObject



73
74
75
# File 'lib/rspec/matchers/built_in/raise_error.rb', line 73

def description
  "raise #{expected_error}"
end

#does_not_match?(given_proc) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/rspec/matchers/built_in/raise_error.rb', line 40

def does_not_match?(given_proc)
  !matches?(given_proc, :negative_expectation)
end

#eval_blockObject



44
45
46
47
48
49
50
51
52
# File 'lib/rspec/matchers/built_in/raise_error.rb', line 44

def eval_block
  @eval_block = true
  begin
    @block[@actual_error]
    @eval_block_passed = true
  rescue Exception => err
    @actual_error = err
  end
end

#failure_message_for_shouldObject



65
66
67
# File 'lib/rspec/matchers/built_in/raise_error.rb', line 65

def failure_message_for_should
  @eval_block ? @actual_error.message : "expected #{expected_error}#{given_error}"
end

#failure_message_for_should_notObject



69
70
71
# File 'lib/rspec/matchers/built_in/raise_error.rb', line 69

def failure_message_for_should_not
  "expected no #{expected_error}#{given_error}"
end

#matches?(given_proc, negative_expectation = false) ⇒ Boolean Also known as: ==

Returns:

  • (Boolean)


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rspec/matchers/built_in/raise_error.rb', line 16

def matches?(given_proc, negative_expectation = false)
  @raised_expected_error = false
  @with_expected_message = false
  @eval_block = false
  @eval_block_passed = false
  begin
    given_proc.call
  rescue @expected_error => @actual_error
    @raised_expected_error = true
    @with_expected_message = verify_message
  rescue Exception => @actual_error
    # This clause should be empty, but rcov will not report it as covered
    # unless something (anything) is executed within the clause
    "http://eigenclass.org/hiki.rb?rcov-0.8.0"
  end

  unless negative_expectation
    eval_block if @raised_expected_error && @with_expected_message && @block
  end
ensure
  return (@raised_expected_error & @with_expected_message) ? (@eval_block ? @eval_block_passed : true) : false
end

#verify_messageObject



54
55
56
57
58
59
60
61
62
63
# File 'lib/rspec/matchers/built_in/raise_error.rb', line 54

def verify_message
  case @expected_message
  when nil
    true
  when Regexp
    @expected_message =~ @actual_error.message
  else
    @expected_message == @actual_error.message
  end
end