Skip to content

Instantly share code, notes, and snippets.

@caius
Last active September 24, 2020 16:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save caius/28bc35a2aef2b252c65e397a478c2e07 to your computer and use it in GitHub Desktop.
Save caius/28bc35a2aef2b252c65e397a478c2e07 to your computer and use it in GitHub Desktop.
require "net/http"
require "json"
require "rspec/autorun"
module Thing
def self.thing(response)
case response
when Net::HTTPSuccess
:ok
when Net::HTTPNotFound
:Nope
else
:fail
end
end
end
RSpec.describe "thing" do
subject { Thing.thing(response) }
context "with success" do
let(:response) { Net::HTTPSuccess.new("1.1", "200", "OK") }
before { allow(response).to receive(:body).and_return({status: :stuff}.to_json) }
it "does a thing" do
expect(subject).to eq(:ok)
end
end
context "with Nope" do
let(:response) { Net::HTTPNotFound.new("1.1", "404", "Not Found") }
it "errors" do
expect(subject).to eq(:Nope)
end
end
context "with fail" do
let(:response) { Net::HTTPServiceUnavailable.new("1.1", "503", "Service Unavailable") }
it "errors" do
expect(subject).to eq(:fail)
end
end
end
# >> ...
# >>
# >> Finished in 0.01397 seconds (files took 0.15176 seconds to load)
# >> 3 examples, 0 failures
# >>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment