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

  • Home
  • SEARCH
  • 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 153563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T09:49:23+00:00 2026-05-11T09:49:23+00:00

I want to be able to spec out that when Open-Uri open() calls either

  • 0

I want to be able to spec out that when Open-Uri open() calls either timeout or raise an exception such as SocketError I am handling things as expected, however I’m having trouble with this.

Here is my spec (for SocketError):

@obj.should_receive(:open).with('some_url').and_raise(SocketError) 

And the part of my object where I’m using open-uri:

begin   resp = open(url)   resp = resp.read rescue SocketError   something = true end 

However in this situation the spec fails as with a nil.read error.

This is the second time this week I’ve come across this problem, the previous time I was attempting to simulate a TimeoutError when wrapping open() with a timeout() {}, that time I gave up and just caused an actual timeout to happen by opening up the class. I could obviously cause this to throw a SocketError by trying to call an invalid URL, but I’m sure there is a correct way to mock this out with RSpec.

Update: I obviously wasn’t thinking clearly that late at night, the error was actually when I re-tried the URL after the SocketError, the and_raise(SocketError) part worked fine.

  • 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. 2026-05-11T09:49:23+00:00Added an answer on May 11, 2026 at 9:49 am

    The line you provided should work, based on the information you’ve given: I made a tiny test class and spec (see below) with only the described functionality, and things behaved as expected. It might be helpful if you could provide a little more context – the full ‘it’ block from the spec, for instance, might expose some other problem.

    As mentioned, the following spec passes, and I believe it captures the logic you were attempting to verify:

    require 'rubygems' require 'spec'  class Foo   attr_accessor :socket_error    def get(url)     @socket_error = false     begin       resp = open(url)       resp = resp.read     rescue SocketError       @socket_error = true     end   end end  describe Foo do   before do     @foo = Foo.new   end    it 'should handle socket errors' do     @foo.should_receive(:open).with('http://www.google.com').and_raise(SocketError)     @foo.get('http://www.google.com')     @foo.socket_error.should be_true   end end 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to be able to capture the exception that is thrown when a
I want to be able to run a single spec file's tests — for
I want to able to open a command prompt at the folder which contains
I have this restfull service that I want to be able to call for
I want to make AJAX calls to a secure server that uses a self-signed
I'm using LaTeX and BibTeX for an article, and I want to able to
Want to be able to provide a search interface for a collection of objects
I want to be able to bind an event to the axes in high
I want to be able to deply a jruby rails 3.1 app to Heroku.
i want to be able to make an IBaction execute when i click on

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.