Skip to content

Instantly share code, notes, and snippets.

@bsodmike
Created February 23, 2021 08:34
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 bsodmike/70cf0871b4d999ed0edd04c0b558177d to your computer and use it in GitHub Desktop.
Save bsodmike/70cf0871b4d999ed0edd04c0b558177d to your computer and use it in GitHub Desktop.
RSpec example using Webmock to stub and API request
require 'faraday'
require 'rspec'
require 'webmock/rspec'
class MembersApiClient
def self.fetch(url)
Faraday.get(url)
end
end
describe MembersApiClient do
describe 'fetching API version' do
let(:api_version_url) { 'https://dummy.restapiexample.com/api/v1' }
let(:response) do
MembersApiClient.fetch(api_version_url)
end
it "returns a 404" do
stub_request(:get, api_version_url)
.to_return(status: 404)
expect(response.status).to eq(404)
end
end
end
# frozen_string_literal: true
source "https://rubygems.org"
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
gem "faraday"
gem "json"
group :test do
gem "rspec"
gem "webmock"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment