require 'rails_helper' RSpec.describe PokemonController, type: :controller do describe "DELETE #destroy" do let!(:pokemon) { FactoryBot.create(:pokemon) } subject { delete :destroy, params: {id: pokemon.id} } context "the pokemon is successfully deleted" do it 'deletes the pokemon' do expect{subject}.to change{Pokemon.count}.by(-1) expect(response).to redirect_to pokemon_index_path end end context "the pokemon is not deleted" do before { allow_any_instance_of(Pokemon).to receive(:destroy).and_return(false) } it 'does not delete the pokemon' do expect{subject}.to change{Pokemon.count}.by(0) expect(response).to render_template :show end end end describe "POST #upload" do subject { post :upload, params: {pokemon: double()} } let(:pokemon) { double(pokedex_number: 10, nickname: "pyukuchu", to_h: {pokedex_number: 10, nickname: "pyukuchu"}) } let(:client_response) { double(pokemon: [pokemon]) } before do allow_any_instance_of(PKParse::Client).to receive(:parse).and_return(client_response) end context 'parsing succeeds' do context 'committing result to database fails' do before { allow(Pokemon).to receive(:create!).and_raise(ActiveRecord::ActiveRecordError.new) } it 'does not create any new pokemon' do #expect{subject}.to raise_error 'canned failure' expect{(subject rescue nil)}.to change{Pokemon.count}.by(0) expect(response).to render_template :new end end context 'committing result to database succeeds' do before do allow_any_instance_of(PKParse::Client).to receive(:parse).and_return(client_response) end it 'creates some new pokemon' do expect{subject}.to change{Pokemon.count}.by(1) expect(response).to redirect_to pokemon_index_path end end end context 'parsing fails' do before do allow_any_instance_of(PKParse::Client).to receive(:parse).and_raise(PKParse::Error.new(StandardError.new)) end it 'does not create any new pokemon' do expect{subject}.to change{Pokemon.count}.by(0) expect(controller.flash.now["alert"]).not_to be_empty expect(response).to render_template :new end end end end