generic_test_helper.rb
require 'flexmock'
module GenericTestHelper
include FlexMock::TestCase
TIMESTAMP_MATCHER = '\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}[+]\d{2}:\d{2}'
def clear_mocked_method(mock, method)
return unless mock.is_a? FlexMock
mock.instance_eval { @expectations.delete method.to_sym }
end
def mock_method(mock, method, retval, with = nil)
mock = flexmock mock unless mock.is_a? FlexMock
clear_mocked_method mock, method
if with.nil?
mock.should_receive(method).and_return retval
else
mock.should_receive(method).with(with).and_return retval
end
end
end
foo_test.rb
require 'generic_test_helper'
class FooTest < Test::Unit::TestCase
include GenericTestHelper
def test_exceptions_are_logged
exception = nil
begin
1 / 0
rescue Exception => exception
end
mock_database_lookups
clear_mocked_method Bar.instance_eval{@flexmock_proxy}.mock, :find_by_id
flexmock(Bar).should_receive(:find_by_id).and_raise(exception)
assert_nothing_raised { Foo.run 0 }
assert_match /exception caught: #{Regexp.escape exception.message} #{exception.backtrace.map{|str| Regexp.escape(str)}.join "\n#{TIMESTAMP_MATCHER}" }/,
@stdout.string
end
end
No comments:
Post a Comment