Created
September 6, 2012 19:50
-
-
Save steveburkett/3659891 to your computer and use it in GitHub Desktop.
testing ensure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TolerantFetcher | |
def self.get(url) | |
TimeoutNinja.try do | |
timeout(20) do | |
begin | |
handle = open(url) | |
response = handle.read | |
ensure | |
handle.try(:close!) ####### HERE | |
end | |
log_response(url, response) if debugging_enabled? | |
response | |
end | |
end | |
end | |
require 'spec_helper' | |
describe TolerantFetcher, :suppress_timeout_ninja_delay do | |
let(:response) { "Response Text" } | |
let(:handle) { double(read: "Response Text") } | |
subject { TolerantFetcher.get("example.com") } | |
before { handle.stub(:close!) } | |
describe ".get" do | |
context "when debugging is disabled" do | |
before { TolerantFetcher.stub(debugging_enabled?: false) } | |
context "when the open call returns a response" do | |
before do | |
TolerantFetcher.stub(open: handle) | |
end | |
it "does not attempt to log the response" do | |
TolerantFetcher.should_not_receive(:log_response).with(:anything) | |
end | |
it "should receive a close!" do | |
handle.should_receive(:close!) | |
subject | |
end | |
it "calls open with the passed-in URL and a timeout" do | |
TolerantFetcher | |
.should_receive(:open) | |
.with("example.com") | |
.and_return(handle) | |
subject | |
end | |
it "returns the response" do | |
subject.should == "Response Text" | |
end | |
context "when the read raises exception" do | |
before do | |
handle.stub(:read).and_raise(Exception) | |
end | |
it "should call close!" do | |
handle.should_receive(:close!) | |
#expect { subject }.to raise_error(Exception) #this works...but i'm not testing this | |
subject rescue nil ### TIML: this doesnt work. the test ends when the exception is thrown | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment