Sha256: 63e781e13ab482a7daa5ef4309c349ae659355e2d56930468dd56934ec007921

Contents?: true

Size: 1.39 KB

Versions: 32

Compression:

Stored size: 1.39 KB

Contents

# frozen_string_literal: true

# Requires Ruby with rspec and faraday gems.
# rspec client_spec.rb

require 'faraday'
require 'json'

# Example API client
class Client
  def initialize(conn)
    @conn = conn
  end

  def sushi(jname)
    res = @conn.get("/#{jname}")
    data = JSON.parse(res.body)
    data['name']
  end
end

RSpec.describe Client do
  let(:stubs)  { Faraday::Adapter::Test::Stubs.new }
  let(:conn)   { Faraday.new { |b| b.adapter(:test, stubs) } }
  let(:client) { Client.new(conn) }

  it 'parses name' do
    stubs.get('/ebi') do |env|
      # optional: you can inspect the Faraday::Env
      expect(env.url.path).to eq('/ebi')
      [
        200,
        { 'Content-Type': 'application/javascript' },
        '{"name": "shrimp"}'
      ]
    end

    # uncomment to trigger stubs.verify_stubbed_calls failure
    # stubs.get('/unused') { [404, {}, ''] }

    expect(client.sushi('ebi')).to eq('shrimp')
    stubs.verify_stubbed_calls
  end

  it 'handles 404' do
    stubs.get('/ebi') do
      [
        404,
        { 'Content-Type': 'application/javascript' },
        '{}'
      ]
    end
    expect(client.sushi('ebi')).to be_nil
    stubs.verify_stubbed_calls
  end

  it 'handles exception' do
    stubs.get('/ebi') do
      raise Faraday::ConnectionFailed, nil
    end

    expect { client.sushi('ebi') }.to raise_error(Faraday::ConnectionFailed)
    stubs.verify_stubbed_calls
  end
end

Version data entries

32 entries across 23 versions & 6 rubygems

Version Path
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/faraday-1.2.0/examples/client_spec.rb
tdiary-5.2.0 vendor/bundle/ruby/2.7.0/gems/faraday-1.4.2/examples/client_spec.rb
tdiary-5.1.7 vendor/bundle/ruby/2.7.0/gems/faraday-1.4.2/examples/client_spec.rb
faraday-1.6.0 examples/client_spec.rb
faraday-1.5.1 examples/client_spec.rb
faraday-1.5.0 examples/client_spec.rb
faraday-1.4.3 examples/client_spec.rb
passbase-1.3.0 vendor/bundle/ruby/2.7.0/gems/faraday-1.4.2/examples/client_spec.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/faraday-1.3.0/examples/client_spec.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/faraday-1.4.2/examples/client_spec.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/tdiary-5.1.5/vendor/bundle/ruby/2.7.0/gems/faraday-1.3.0/examples/client_spec.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/tdiary-5.1.5/vendor/bundle/ruby/3.0.0/gems/faraday-1.1.0/examples/client_spec.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/tdiary-5.1.5/vendor/bundle/ruby/3.0.0/gems/faraday-1.2.0/examples/client_spec.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/tdiary-5.1.5/vendor/bundle/ruby/3.0.0/gems/tdiary-5.1.4/vendor/bundle/ruby/2.7.0/gems/faraday-1.1.0/examples/client_spec.rb
tdiary-5.1.6 vendor/bundle/ruby/3.0.0/gems/faraday-1.4.2/examples/client_spec.rb
faraday-1.4.2 examples/client_spec.rb
cocRb-0.1.0 .bundle/ruby/3.0.0/gems/faraday-1.4.1/examples/client_spec.rb
faraday-1.4.1 examples/client_spec.rb
faraday-1.4.0 examples/client_spec.rb
faraday-1.3.1 examples/client_spec.rb