Hi, I wrote a small program that uses Mechanize to traverse a site.
I want to write tests for it, but don't want it to actually go log onto the site every time I run the tests. I would like to mock the internet, so that when it goes to some site, it simply returns the stored results.
Here is a small example, pretend my code's purpose was to pull links off of the google homepage, so I write a test to ensure the first link my code finds has the text "Images". I might write something like this:
require 'rubygems'
require 'mechanize'
require 'test/unit'
def my_code_to_find_links
google = WWW::Mechanize.new.get('http://www.google.com')
# ...
# some code to figure out which liks it wants
# ...
google.links
end
class TestGoogle < Test::Unit::TestCase
def test_first_link_is_images
assert_equal 'Images' , my_code_to_find_links.first.text
end
end
How do I mock google.com so that I can test my_code_to_find_links without all the overhead of actually accessing the internet?
thanks -Josh