Sha256: 0ee442982151e7a7be2f9478c79d5f54457b8633ea530b02a42a69fbe280fce8

Contents?: true

Size: 1.61 KB

Versions: 2

Compression:

Stored size: 1.61 KB

Contents

require "spec_helper"

describe Myfinance::Response do
  subject { described_class.new(response) }

  describe "#resolve!" do
    context 'when success' do
      let(:response) { double(success?: true) }

      it 'returns self' do
        expect(subject.resolve!).to eq(response)
      end

      context 'when block given' do
        let(:response) { double(success?: true) }

        it 'returns block' do
          expect(subject.resolve!{ response }).to eq(response)
        end
      end
    end

    context 'when timeout' do
      let(:response) { double(success?: false, timed_out?: true) }

      it 'raises RequestTimeout' do
        expect { subject.resolve! }.to raise_error(Myfinance::RequestTimeout)
      end
    end

    context 'when not success neither timeout' do
      let(:response) { double(success?: false, timed_out?: false, code: 301, status_message: 'Moved Permanently', body: '') }

      it 'raises RequestError' do
        expect { subject.resolve! }.to raise_error(Myfinance::RequestError)
      end
    end
  end

  describe "#parsed_body" do
    let(:response) { double(success?: true) }

    context "when JSON is invalid" do
      it "returns empty hash" do
        allow(subject).to receive(:body).and_return("{\"key\": ")
        expect(subject.parsed_body).to eq({})
      end
    end

    context "when JSON is valid" do
      it "returns empty hash" do
        allow(subject).to receive(:body).and_return("{\"key\": \"value\"}")
        expect(MultiJson).to receive(:load).with("{\"key\": \"value\"}").and_call_original
        expect(subject.parsed_body).to eq({ "key" => "value" })
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
myfinance-0.2.0 spec/lib/myfinance/response_spec.rb
myfinance-0.1.0 spec/lib/myfinance/response_spec.rb