# 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 subject(:parse) { client.parse_files(files) } let!(:files) { [] } context 'when succeeds as normal' do specify { expect(parse.pokemon.size).to eq 1 } end context 'when fails' do context 'when getting a response' do before do allow(RestClient) .to receive(:post).and_raise(RestClient::Exception.new) end it 'returns a more specific error' do expect { parse }.to raise_error(PKParse::ResponseError) end end context 'when processing a response' do before do allow(JSON) .to receive(:parse).and_raise(JSON::ParserError.new) end it 'returns a more specific error' do expect { parse }.to raise_error(PKParse::Error) end end end end describe '#parse_base64' do subject(:parse_base64) { client.parse_base64(base64_strings) } let!(:base64_strings) { ['dead'] } context 'when succeeds as normal' do specify { expect(parse_base64.pokemon.size).to eq 1 } end context 'when fails' do context 'when getting a response' do before do allow(RestClient) .to receive(:post).and_raise(RestClient::Exception.new) end it 'returns a more specific error' do expect { parse_base64 }.to raise_error(PKParse::ResponseError) end end context 'when processing a response' do before do allow(JSON) .to receive(:parse).and_raise(JSON::ParserError.new) end it 'returns a more specific error' do expect { parse_base64 }.to raise_error(PKParse::Error) end end end end end