Class: RaiseErrorMatcher

Inherits:
Object show all
Defined in:
lib/extensions/rhospec/rhospec.rb,
lib/extensions/mspec/mspec/matchers/raise_error.rb

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of RaiseErrorMatcher.



383
384
385
386
387
# File 'lib/extensions/rhospec/rhospec.rb', line 383

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

Instance Method Details

#exception_class_and_message(exception_class, message) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/extensions/mspec/mspec/matchers/raise_error.rb', line 38

def exception_class_and_message(exception_class, message)
  if message
    "#{exception_class} (#{message})"
  else
    "#{exception_class}"
  end
end

#failure_messageObject



409
410
411
412
413
414
415
416
417
418
419
# File 'lib/extensions/rhospec/rhospec.rb', line 409

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"
  end

  message
end

#format_exception(exception) ⇒ Object



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

def format_exception(exception)
  exception_class_and_message(exception.class, exception.message)
end

#format_expected_exceptionObject



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

def format_expected_exception
  exception_class_and_message(@exception, @message)
end

#matches?(proc) ⇒ Boolean

Returns:

  • (Boolean)


389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
# File 'lib/extensions/rhospec/rhospec.rb', line 389

def matches?(proc)
  proc.call
  return false
rescue Exception => err
  @actual = err
  return false unless @exception === @actual
  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

#matching_exception?(exc) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/extensions/mspec/mspec/matchers/raise_error.rb', line 21

def matching_exception?(exc)
  return false unless @exception === exc
  if @message then
    case @message
    when String
      return false if @message != exc.message
    when Regexp
      return false if @message !~ exc.message
    end
  end

  # The block has its own expectations and will throw an exception if it fails
  @block[exc] if @block

  return true
end

#negative_failure_messageObject



421
422
423
# File 'lib/extensions/rhospec/rhospec.rb', line 421

def negative_failure_message
  ["Expected to not get #{@exception}#{%[ (#{@message})] if @message}", ""]
end