pokemon_controller_spec.rb 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. require 'rails_helper'
  2. RSpec.describe PokemonController, type: :controller do
  3. describe "DELETE #destroy" do
  4. let!(:pokemon) { FactoryBot.create(:pokemon) }
  5. subject { delete :destroy, params: {id: pokemon.id} }
  6. context "the pokemon is successfully deleted" do
  7. it 'deletes the pokemon' do
  8. expect{subject}.to change{Pokemon.count}.by(-1)
  9. expect(response).to redirect_to pokemon_index_path
  10. end
  11. end
  12. context "the pokemon is not deleted" do
  13. before { allow_any_instance_of(Pokemon).to receive(:destroy).and_return(false) }
  14. it 'does not delete the pokemon' do
  15. expect{subject}.to change{Pokemon.count}.by(0)
  16. expect(response).to render_template :show
  17. end
  18. end
  19. end
  20. describe "POST #upload" do
  21. subject { post :upload, params: {pokemon: double()} }
  22. let(:pokemon) { double(pokedex_number: 10, nickname: "pyukuchu", to_h: {pokedex_number: 10, nickname: "pyukuchu"}) }
  23. let(:client_response) { double(pokemon: [pokemon]) }
  24. before do
  25. allow_any_instance_of(PKParse::Client).to receive(:parse).and_return(client_response)
  26. end
  27. context 'parsing succeeds' do
  28. context 'committing result to database fails' do
  29. before { allow(Pokemon).to receive(:create!).and_raise(ActiveRecord::ActiveRecordError.new) }
  30. it 'does not create any new pokemon' do
  31. #expect{subject}.to raise_error 'canned failure'
  32. expect{(subject rescue nil)}.to change{Pokemon.count}.by(0)
  33. expect(response).to render_template :new
  34. end
  35. end
  36. context 'committing result to database succeeds' do
  37. before do
  38. allow_any_instance_of(PKParse::Client).to receive(:parse).and_return(client_response)
  39. end
  40. it 'creates some new pokemon' do
  41. expect{subject}.to change{Pokemon.count}.by(1)
  42. expect(response).to redirect_to pokemon_index_path
  43. end
  44. end
  45. end
  46. context 'parsing fails' do
  47. before do
  48. allow_any_instance_of(PKParse::Client).to receive(:parse).and_raise(PKParse::Error.new(StandardError.new))
  49. end
  50. it 'does not create any new pokemon' do
  51. expect{subject}.to change{Pokemon.count}.by(0)
  52. expect(controller.flash.now["alert"]).not_to be_empty
  53. expect(response).to render_template :new
  54. end
  55. end
  56. end
  57. end