# frozen_string_literal: true require 'rails_helper' RSpec.describe PKParse::Client do let(:client) { described_class.new } let(:response) { '[{"pokedex_number":1}]' } before { allow(RestClient).to receive(:post).and_return(response) } describe '#parse_files' do let!(:files) { [] } subject { client.parse_files(files) } context 'succeeds as normal' do specify { expect(subject.pokemon.size).to eq 1 } end context 'fails' do context 'getting a response' do before { allow(RestClient).to receive(:post).and_raise(RestClient::Exception.new) } it 'returns a more specific error' do expect { subject }.to raise_error(PKParse::ResponseError) end end context 'processing a response' do before { allow(JSON).to receive(:parse).and_raise(JSON::ParserError.new) } it 'returns a more specific error' do expect { subject }.to raise_error(PKParse::Error) end end end end describe '#parse_base64' do let!(:base64_strings) { ['dead'] } subject { client.parse_base64(base64_strings) } context 'succeeds as normal' do specify { expect(subject.pokemon.size).to eq 1 } end context 'fails' do context 'getting a response' do before { allow(RestClient).to receive(:post).and_raise(RestClient::Exception.new) } it 'returns a more specific error' do expect { subject }.to raise_error(PKParse::ResponseError) end end context 'processing a response' do before { allow(JSON).to receive(:parse).and_raise(JSON::ParserError.new) } it 'returns a more specific error' do expect { subject }.to raise_error(PKParse::Error) end end end end end