client_spec.rb 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe PKParse::Client do
  4. let(:client) { described_class.new }
  5. let(:response) { '[{"pokedex_number":1}]' }
  6. before { allow(RestClient).to receive(:post).and_return(response) }
  7. describe '#parse_files' do
  8. let!(:files) { [] }
  9. subject { client.parse_files(files) }
  10. context 'succeeds as normal' do
  11. specify { expect(subject.pokemon.size).to eq 1 }
  12. end
  13. context 'fails' do
  14. context 'getting a response' do
  15. before { allow(RestClient).to receive(:post).and_raise(RestClient::Exception.new) }
  16. it 'returns a more specific error' do
  17. expect { subject }.to raise_error(PKParse::ResponseError)
  18. end
  19. end
  20. context 'processing a response' do
  21. before { allow(JSON).to receive(:parse).and_raise(JSON::ParserError.new) }
  22. it 'returns a more specific error' do
  23. expect { subject }.to raise_error(PKParse::Error)
  24. end
  25. end
  26. end
  27. end
  28. describe '#parse_base64' do
  29. let!(:base64_strings) { ['dead'] }
  30. subject { client.parse_base64(base64_strings) }
  31. context 'succeeds as normal' do
  32. specify { expect(subject.pokemon.size).to eq 1 }
  33. end
  34. context 'fails' do
  35. context 'getting a response' do
  36. before { allow(RestClient).to receive(:post).and_raise(RestClient::Exception.new) }
  37. it 'returns a more specific error' do
  38. expect { subject }.to raise_error(PKParse::ResponseError)
  39. end
  40. end
  41. context 'processing a response' do
  42. before { allow(JSON).to receive(:parse).and_raise(JSON::ParserError.new) }
  43. it 'returns a more specific error' do
  44. expect { subject }.to raise_error(PKParse::Error)
  45. end
  46. end
  47. end
  48. end
  49. end