I want to be able to indicate that when Open-Uri open () either calls a timeout or throws an exception such as a SocketError, I handle things as expected, however I am having problems 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 use open-uri:
begin resp = open(url) resp = resp.read rescue SocketError something = true end
However, in this situation, the specification does not work, as with the nil.read error.
This is the second time this week that I encountered this problem, in the previous time I tried to simulate a TimeoutError when wrapping open() with timeout() {} , while I gave up and just called the actual timeout to happen by opening class. I obviously could get this to throw a SocketError by trying to raise an invalid url, but I'm sure there is a proper way to mock this with RSpec.
Update: I obviously did not think clearly that late at night, the error was actually when I re-tried the URL after the SocketError, and_raise (SocketError) part was working fine.
source share