Sha256: 789d89af729635832e05ff1b0b2dfd0168196f8256a1098cd46fb4a5d5c16a5e

Contents?: true

Size: 1.16 KB

Versions: 3

Compression:

Stored size: 1.16 KB

Contents

require 'debug'
require 'spec_helper'
require 'webmock/rspec'
require './lib/latest_stock_price/price_all'

RSpec.describe LatestStockPrice::PriceAll do
  let(:uri) { URI("https://latest-stock-price.p.rapidapi.com/any") }
  let(:valid_response) { { "key" => "value" }.to_json }

  before do
    ENV['X_RAPIDAPI_KEY'] = 'dummy_key'
    ENV['X_RAPIDAPI_HOST'] = 'dummy_host'
  end

  describe '.fetch' do
    context 'when the request is successful' do
      before do
        stub_request(:get, uri)
          .with(
            headers: {
              'x-rapidapi-key' => ENV['X_RAPIDAPI_KEY'],
              'x-rapidapi-host' => ENV['X_RAPIDAPI_HOST']
            }
          )
          .to_return(status: 200, body: valid_response)
      end

      it 'returns parsed JSON response' do
        response = described_class.fetch
        expect(response).to eq({"key" => "value"})
      end
    end

    context 'when the request fails' do
      before do
        stub_request(:get, uri).to_return(status: 404, body: "")
      end

      it 'raises an error' do
        expect { described_class.fetch }.to raise_error(RuntimeError, /Error fetching data: 404/)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
latest_stock_price-1.0.2 spec/price_all_spec.rb
latest_stock_price-1.0.1 spec/price_all_spec.rb
latest_stock_price-1.0.0 spec/price_all_spec.rb