Is there an existing way to assert that abort is invoked with
Test::Unit. Here's what I came up with.

Place in "test_helper.rb" for example.

------------------------------------------------------
# Three redefinitions to be able to assert aborts.
class AbortException < Exception
end

class Test::Unit::TestCase
  def assert_aborts(msg_or_pattern)
    asserted = false
    caught_exception = 'none'
    begin
      yield if block_given? # if there is no block, there will not be
any abort
either
    rescue AbortException => e
      caught_exception = e
      if msg_or_pattern.is_a? String
        assert_equal msg_or_pattern, e.to_s.sub(/^[a-z_]*: /,'')
        return
      end
      if msg_or_pattern.is_a? Regexp
        assert_match msg_or_pattern, e.to_s
        return
      end
    end
    flunk "Expected to handle abort with >>#{ msg_or_pattern }<<. Caught
exception >>#{ caught_exception }<< but didn't handle"
  end
end

module Kernel
  def abort(msg)
    raise AbortException.new(msg)
  end
end
------------------------------------------------------

Then in a test

def test_aborting
  assert_aborts 'fatal error encountered' do
    trigger_abort # ....
  end
end




Stephan
-- 
Posted via http://www.ruby-forum.com/.