Module: RubyUnit::Assertions::Exceptions

Includes:
RubyUnit::AssertionMessage, Root
Included in:
RubyUnit::Assertions
Defined in:
lib/RubyUnit/Assertions/Exceptions.rb

Constant Summary

Constants included from RubyUnit::AssertionMessage

RubyUnit::AssertionMessage::ASSERT_CLASS_METHOD_ERROR, RubyUnit::AssertionMessage::ASSERT_CONST_DEFINED_ERROR, RubyUnit::AssertionMessage::ASSERT_CONST_ERROR, RubyUnit::AssertionMessage::ASSERT_CONST_NOT_DEFINED_ERROR, RubyUnit::AssertionMessage::ASSERT_DESCENDENT_ERROR, RubyUnit::AssertionMessage::ASSERT_EMPTY_ERROR, RubyUnit::AssertionMessage::ASSERT_EQUAL_ERROR, RubyUnit::AssertionMessage::ASSERT_ERROR, RubyUnit::AssertionMessage::ASSERT_FALSE_ERROR, RubyUnit::AssertionMessage::ASSERT_GREATERTHANOREQUAL_ERROR, RubyUnit::AssertionMessage::ASSERT_GREATERTHAN_ERROR, RubyUnit::AssertionMessage::ASSERT_INCLUDE_ERROR, RubyUnit::AssertionMessage::ASSERT_INSTANCE_METHOD_ERROR, RubyUnit::AssertionMessage::ASSERT_INSTANCE_OF_ERROR, RubyUnit::AssertionMessage::ASSERT_INSTANCE_VARIABLE_DEFINED_ERROR, RubyUnit::AssertionMessage::ASSERT_INSTANCE_VARIABLE_EQUAL_ERROR, RubyUnit::AssertionMessage::ASSERT_INSTANCE_VARIABLE_KIND_OF_ERROR, RubyUnit::AssertionMessage::ASSERT_INSTANCE_VARIABLE_NOT_DEFINED_ERROR, RubyUnit::AssertionMessage::ASSERT_INSTANCE_VARIABLE_NOT_EQUAL_ERROR, RubyUnit::AssertionMessage::ASSERT_INSTANCE_VARIABLE_NOT_KIND_OF_ERROR, RubyUnit::AssertionMessage::ASSERT_KIND_OF_ERROR, RubyUnit::AssertionMessage::ASSERT_LESSTHANOREQUAL_ERROR, RubyUnit::AssertionMessage::ASSERT_LESSTHAN_ERROR, RubyUnit::AssertionMessage::ASSERT_MATCH_ERROR, RubyUnit::AssertionMessage::ASSERT_METHOD_ERROR, RubyUnit::AssertionMessage::ASSERT_NIL_ERROR, RubyUnit::AssertionMessage::ASSERT_NOTHING_RAISED_ERROR, RubyUnit::AssertionMessage::ASSERT_NOT_CLASS_METHOD_ERROR, RubyUnit::AssertionMessage::ASSERT_NOT_DESCENDENT_ERROR, RubyUnit::AssertionMessage::ASSERT_NOT_EMPTY_ERROR, RubyUnit::AssertionMessage::ASSERT_NOT_EQUAL_ERROR, RubyUnit::AssertionMessage::ASSERT_NOT_ERROR, RubyUnit::AssertionMessage::ASSERT_NOT_INCLUDE_ERROR, RubyUnit::AssertionMessage::ASSERT_NOT_INSTANCE_METHOD_ERROR, RubyUnit::AssertionMessage::ASSERT_NOT_INSTANCE_OF_ERROR, RubyUnit::AssertionMessage::ASSERT_NOT_KIND_OF_ERROR, RubyUnit::AssertionMessage::ASSERT_NOT_MATCH_ERROR, RubyUnit::AssertionMessage::ASSERT_NOT_METHOD_ERROR, RubyUnit::AssertionMessage::ASSERT_NOT_NIL_ERROR, RubyUnit::AssertionMessage::ASSERT_NOT_RESPOND_TO_ERROR, RubyUnit::AssertionMessage::ASSERT_NOT_SAME_ERROR, RubyUnit::AssertionMessage::ASSERT_RAISE_ERROR, RubyUnit::AssertionMessage::ASSERT_RAISE_EXPECTED_ERROR, RubyUnit::AssertionMessage::ASSERT_RAISE_KIND_OF_ERROR, RubyUnit::AssertionMessage::ASSERT_RAISE_MESSAGE_ERROR, RubyUnit::AssertionMessage::ASSERT_RESPOND_TO_ERROR, RubyUnit::AssertionMessage::ASSERT_SAME_ERROR, RubyUnit::AssertionMessage::ASSERT_TRUE_ERROR, RubyUnit::AssertionMessage::FAILING, RubyUnit::AssertionMessage::FAILURE

Instance Method Summary collapse

Instance Method Details

#assertNothingRaised(message = nil, &block) ⇒ Object

Assert that no exception is raised.

  • raises RubyUnit::AssertionFailure if any exception is raised

message

The message provided to be reported for a failure

&block

The code block that is executed

assertNothingRaised 'Not expecting an exception!' do
  # do something
end


23
24
25
26
27
28
29
30
31
32
# File 'lib/RubyUnit/Assertions/Exceptions.rb', line 23

def assertNothingRaised message = nil, &block
  __assert_block do
    begin
      yield
      true # just in case the block yields 'false' or 'nil'
    rescue Exception => e
      __fail ASSERT_NOTHING_RAISED_ERROR, message, {:exception=>e.message}
    end
  end
end

#assertRaiseExpected(exception, pattern, message = nil, &block) ⇒ Object

Assert that a specified exception is raised.

  • raises RubyUnit::AssertionFailure unless the correct Exception is raised

exception

The Exception class that is expected.

pattern

The String or Regexp that will be used to validate the Exception message

message

The message provided to be reported for a failure

&block

The code block that is expected to throw the Exception

assertRaiseExpected StandardError, /^Invalid/, 'Expecting an exception!' do
  raise StandardError, 'Invalid Retroincabulator'
end


96
97
98
# File 'lib/RubyUnit/Assertions/Exceptions.rb', line 96

def assertRaiseExpected exception, pattern, message = nil, &block
  __assert_exception ASSERT_RAISE_EXPECTED_ERROR, exception, pattern, message, &block
end

#assertRaiseKindOf(exception, message = nil, &block) ⇒ Object

Assert that a specified exception type is raised.

  • raises RubyUnit::AssertionFailure unless the correct Exception type is raised

exception

The Exception class that is expected.

message

The message provided to be reported for a failure

&block

The code block that is expected to throw the Exception

assertRaiseKindOf StandardError, 'Expecting an exception!' do # => fail
  # do something
end


72
73
74
# File 'lib/RubyUnit/Assertions/Exceptions.rb', line 72

def assertRaiseKindOf exception, message = nil, &block
  __assert_exception ASSERT_RAISE_KIND_OF_ERROR, exception, '', message, &block
end

#assertRaiseMessage(pattern, message = nil, &block) ⇒ Object

Assert that a specified exception message is raised.

  • raises RubyUnit::AssertionFailure unless the correct Exception message is raised

pattern

The String or Regexp that will be used to validate the Exception message

message

The message provided to be reported for a failure

&block

The code block that is expected to throw the Exception

assertRaiseMessage /^Invalid/, 'Expecting an exception!' do
  # do something
end


51
52
53
# File 'lib/RubyUnit/Assertions/Exceptions.rb', line 51

def assertRaiseMessage pattern, message = nil, &block
  __assert_exception ASSERT_RAISE_MESSAGE_ERROR, Exception, pattern, message, &block
end