Last active
September 24, 2020 16:25
-
-
Save caius/28bc35a2aef2b252c65e397a478c2e07 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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