I've been working with the Mechanize gem a lot recently and would like to incorporate some tests to ensure I am catching the proper errors. What is the proper way to test error handling?
This is my basic method:
def get(str)
url = format_url(str)
#puts "sending GET request to: #{url}"
sleep(0.1)
@page = Mechanize.new do |a|
a.user_agent_alias = 'Mac Safari'
a.open_timeout = 7
a.read_timeout = 7
a.idle_timeout = 7
a.redirect_ok = true
end.get(url)
rescue Mechanize::ResponseCodeError => e
puts "#{'Response Error:'.red} #{e}"
rescue SocketError => e
puts "#{'Socket Error:'.red} #{e}"
rescue Net::OpenTimeout => e
puts "#{'Connection Timeout:'.red} #{e}"
rescue Errno::ETIMEDOUT => e
puts "#{'Connection Timeout:'.red} #{e}"
rescue Net::HTTP::Persistent::Error
puts "#{'Connection Timeout:'.red} read timeout, too many resets."
end
And this is the start to tests for handling errors:
class TestErrorHandling < Mechanize::TestCase
context 'Example when sending a GET request' do
should 'rescue error and return nil' do
assert_equal nil, Example.get('http://localhost/pagethatdoesntexist')
end
end
end
Am I heading in the correct direction? Any insight and/or resources welcome.