Created
February 23, 2021 08:34
-
-
Save bsodmike/70cf0871b4d999ed0edd04c0b558177d to your computer and use it in GitHub Desktop.
RSpec example using Webmock to stub and API request
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 '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 |
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
# 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