Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 935557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:10:31+00:00 2026-05-15T21:10:31+00:00

I wrote a small program that uses Mechanize to traverse a site. I want

  • 0

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 links 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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T21:10:32+00:00Added an answer on May 15, 2026 at 9:10 pm

    Use Fakeweb to stub out the internet responses.

    For the Google example, first go to the website and save the html of the page you desire. In this case, let’s assume you saved http://www.google.com via your browser or curl. Add the Fakeweb gem to your test.rb file

    Then your code is

    stream = File.read("saved_google_page.html")
    FakeWeb.register_uri(:get, 
        "http://www.google.com", 
        :body => stream, 
        :content_type => "text/html")
    

    When you do your standard Mechanize call of

    agent = Mechanize.New
    page = agent.get("http://www.google.com/")
    

    Fakeweb will return the page you saved with the content_type headers set in a way that Mechanize will think it accessed the internet successfully. Make sure that content_type header is set since otherwise Mechanize treats the response as Mechanize::File instead of Mechanize::Page. You can test that it’s fully working by running tests on your machine with the network connection unplugged.

    p.s. I’m answering this 6 months after the question was asked since this is the top result in Google but it’s unanswered. I just spent 30 minutes figuring this out myself and thought I’d share the solution.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to write a small program that should print something like testing CPU...
I need to write a small program that can detect that it has been
I wrote a small PHP application several months ago that uses the WordPress XMLRPC
I'm developing a small haskell program that uses an external static library I've developed
I am trying to write a small app that uses the subprocess module. My
I would like to write a small program in C# which goes through my
As the title says, I need to write a small program to read data
I wanted to write a small Rapid Serial Visual Presentation (RSVP) program for my
I wrote a small PHP application that I'd like to distribute. I'm looking for
We wrote a small Windows class library that implements extension methods for some standard

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.