title: Rescuing HTTP 404 Mechanize::ResponseCodeError Exceptions with Cucumber, Mechanize, Webrat
body: <p>I was put in charge of testing a non-rails web application using cucumber.  I have basic testing up and running, I.E. I can do things like </p>
  
  <pre><code>Then /^the page should have a header$/ do
    response_body.should have_xpath(%\//header\)
  end
  </code></pre>
  
  <p>The next thing I wanted to test is that non-existent pages, in addition to presenting a friendly error page, are returning the correct http response code (404).</p>
  
  <p>When I <code>visit</code> the 404 page during the cucumber test, the following happens. </p>
  
  <pre><code>Scenario: Visit a url which does not lead to a page # features\404.feature:6
      Given I try to visit a fake page                  # features/step_definitions/404_steps.rb:1
        404 =&gt; Net::HTTPNotFound (Mechanize::ResponseCodeError)
        ./features/step_definitions/404_steps.rb:2:in `/^I try to visit a fake page$/'
        features\404.feature:7:in `Given I try to visit a fake page'
      When the page loads                               # features/step_definitions/home_steps.rb:5
  </code></pre>
  
  <p>This makes sense for testing pages that you expect to exist, but I really would like to be able to test my errors as well. </p>
  
  <p>My question is, how can I rescue the Mechanize::ResponseCodeError so that I may verify the correctness of the 404 error page?</p>
  
  <p>Thanks.</p>