Skip to content
Browse files

Add some tests for the failure scenarios for #recent_jobs

  • Loading branch information...
1 parent fc740ca commit 3f161147470f80afb0a745a2d67b024805c6c5e4 @rtyler committed
Showing with 46 additions and 2 deletions.
  1. +10 −2 lib/saucetv/api.rb
  2. +36 −0 spec/api_spec.rb
View
12 lib/saucetv/api.rb
@@ -32,14 +32,22 @@ def recent_jobs
begin
response = self.class.get("/v1/#{username}/jobs")
rescue Timeout::Error => e
- puts e.inspect
+ puts "A call to SauceTV::API#recent_jobs timed out (#{e.inspect})"
return []
end
unless response.code == 200
return []
end
- response.body
+
+ # I believe that this should only be true if we receive non-JSON back
+ # from the API. Otherwise, I *think* HTTParty will return parsed JSON
+ # (Array)
+ if response.body.instance_of? String
+ []
+ else
+ response.body
+ end
end
end
end
View
36 spec/api_spec.rb
@@ -10,6 +10,42 @@
end
end
+ describe :recent_jobs do
+ context 'when Sauce is timing out' do
+ before :each do
+ subject.class.should_receive(:get) do
+ raise Timeout::Error
+ end
+ end
+
+ it 'should return an empty Array' do
+ jobs = subject.recent_jobs
+ jobs.should_not be_nil
+ jobs.should be_empty
+ end
+ end
+
+ context 'when Sauce is serving HTML' do
+ let(:response) do
+ response = mock('HTTParty::Response')
+ response.stub(:code).and_return(200)
+ markup = "<html><body>Failboat</body></html>"
+ response.stub(:body).and_return(markup)
+ response
+ end
+
+ before :each do
+ subject.class.should_receive(:get).and_return(response)
+ end
+
+ it "should return an empty Array if there's no JSON" do
+ jobs = subject.recent_jobs
+ jobs.should_not be_nil
+ jobs.should be_empty
+ end
+ end
+ end
+
end

0 comments on commit 3f16114

Please sign in to comment.
Something went wrong with that request. Please try again.