| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- # frozen_string_literal: true
- require 'rails_helper'
- RSpec.describe CreatePokemonFromBase64Service, type: :service do
- describe '#execute' do
- subject(:execute) { service.execute(encoded_pokemon) }
- let(:service) { described_class.new }
- let(:encoded_pokemon) { '1234567890abcdef' }
- let(:client_response) { double(pokemon: [pokemon]) }
- let(:pokemon) do
- pkmn = {pokedex_number: 10, nickname: 'pyukuchu'}
- double(**pkmn, to_h: pkmn)
- end
- before do
- allow_any_instance_of(PKParse::Client).to(
- receive(:parse_base64).and_return(client_response),
- )
- allow(service).to receive(:success)
- allow(service).to receive(:error)
- end
- context 'when parsing succeeds' do
- context 'when committing result to database fails' do
- before do
- allow(Pokemon)
- .to receive(:create!).and_raise(ActiveRecord::ActiveRecordError.new)
- end
- it 'does not create any new pokemon' do
- # expect{execute}.to raise_error 'canned failure'
- expect { (execute rescue nil) }.to change(Pokemon, :count).by(0)
- end
- it 'calls #error' do
- execute rescue nil
- expect(service).not_to have_received(:success)
- expect(service).to have_received(:error)
- end
- end
- context 'when committing result to database succeeds' do
- before do
- allow_any_instance_of(PKParse::Client)
- .to receive(:parse_base64).and_return(client_response)
- end
- it 'creates some new pokemon' do
- expect { execute }.to change(Pokemon, :count).by(1)
- end
- it 'calls #success' do
- execute
- expect(service).to have_received(:success).once
- expect(service).not_to have_received(:error)
- end
- end
- end
- context 'when parsing fails' do
- before do
- allow_any_instance_of(PKParse::Client)
- .to(receive(:parse_base64)
- .and_raise(PKParse::Error.new(StandardError.new)))
- end
- it 'does not create any new pokemon' do
- expect { execute }.to change(Pokemon, :count).by(0)
- expect(service).not_to have_received(:success)
- expect(service).to have_received(:error)
- end
- end
- describe '#success' do
- it 'calls super method' do
- allow_any_instance_of(PKParse::Client)
- .to receive(:parse_base64).and_return(client_response)
- expect_any_instance_of(service.class.superclass)
- .to receive(:success).once
- execute
- expect(service).to have_received(:success).once
- end
- end
- end
- end
|