Class: RaiseExceptionMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/mspec/matchers/raise_exception.rb

Instance Method Summary collapse

Constructor Details

#initialize(exception, message, &block) ⇒ RaiseExceptionMatcher

Returns a new instance of RaiseExceptionMatcher.



2
3
4
5
6
# File 'lib/mspec/matchers/raise_exception.rb', line 2

def initialize(exception, message, &block)
  @exception = exception
  @message = message
  @block = block
end

Instance Method Details

#failure_messageObject



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/mspec/matchers/raise_exception.rb', line 27

def failure_message
  message = ["Expected #{@exception}#{%[ (#{@message})] if @message}"]

  if @actual then
    message << "but got #{@actual.class}#{%[ (#{@actual.message})] if @actual.message}"
  else
    message << "but no exception was raised (#@result was returned)"
  end

  message
end

#matches?(proc) ⇒ Boolean

Returns:

  • (Boolean)


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/mspec/matchers/raise_exception.rb', line 8

def matches?(proc)
  @result = proc.call
  return false
rescue Object => @actual
  return false unless @actual.instance_of? @exception
  if @message then
    case @message
    when String then
      return false if @message != @actual.message
    when Regexp then
      return false if @message !~ @actual.message
    end
  end

  @block[@actual] if @block

  return true
end

#negative_failure_messageObject



39
40
41
42
43
# File 'lib/mspec/matchers/raise_exception.rb', line 39

def negative_failure_message
  message = ["Expected to not get #{@exception}#{%[ (#{@message})] if @message}", ""]
  message[1] = "but got #{@actual.class}#{%[ (#{@actual.message})] if @actual.message}" unless @actual.class == @exception
  message
end