pokemon_controller_spec.rb 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe API::V1::PokemonController, type: :controller do
  4. def non_existant_pokemon_id(initial_id = 1)
  5. id = initial_id
  6. id *= 2 until !Pokemon.find_by(id: id)
  7. id
  8. end
  9. describe "DELETE #destroy" do
  10. let!(:pokemon) { FactoryBot.create(:pokemon) }
  11. subject { delete :destroy, params: {id: pokemon.id} }
  12. context "the pokemon is successfully deleted" do
  13. it 'deletes the pokemon' do
  14. expect{subject}.to change{Pokemon.count}.by(-1)
  15. expect(response).to render_template 'api/v1/pokemon/show'
  16. end
  17. end
  18. context "the pokemon is not deleted" do
  19. before { allow_any_instance_of(Pokemon).to receive(:destroy).and_return(false) }
  20. it 'does not delete the pokemon' do
  21. expect{subject}.to change{Pokemon.count}.by(0)
  22. expect(response).to render_template 'api/v1/application/_error'
  23. end
  24. end
  25. context "the pokemon does not exist" do
  26. let(:pokemon) { double(id: non_existant_pokemon_id ) }
  27. it 'raises an error' do
  28. expect{subject rescue nil}.to change{Pokemon.count}.by(0)
  29. expect(response).to render_template 'api/v1/application/_error'
  30. end
  31. end
  32. end
  33. describe "POST #upload" do
  34. subject { post :upload, params: {pokemon: double()} }
  35. let(:pokemon) { double(pokedex_number: 10, nickname: "pyukuchu", to_h: {pokedex_number: 10, nickname: "pyukuchu"}) }
  36. let(:client_response) { double(pokemon: [pokemon]) }
  37. before do
  38. allow_any_instance_of(PKParse::Client).to receive(:parse).and_return(client_response)
  39. end
  40. context 'parsing succeeds' do
  41. context 'committing result to database fails' do
  42. before { allow(Pokemon).to receive(:create!).and_raise(ActiveRecord::ActiveRecordError.new) }
  43. it 'does not create any new pokemon' do
  44. #expect{subject}.to raise_error 'canned failure'
  45. expect{(subject rescue nil)}.to change{Pokemon.count}.by(0)
  46. expect(response).to render_template 'api/v1/application/_error'
  47. end
  48. end
  49. context 'committing result to database succeeds' do
  50. before do
  51. allow_any_instance_of(PKParse::Client).to receive(:parse).and_return(client_response)
  52. end
  53. it 'creates some new pokemon' do
  54. expect{subject}.to change{Pokemon.count}.by(1)
  55. expect(response).to render_template 'api/v1/pokemon/index'
  56. end
  57. end
  58. end
  59. context 'parsing fails' do
  60. before do
  61. allow_any_instance_of(PKParse::Client).to receive(:parse).and_raise(PKParse::Error.new(StandardError.new))
  62. end
  63. it 'does not create any new pokemon' do
  64. expect{subject}.to change{Pokemon.count}.by(0)
  65. expect(response).to render_template 'api/v1/application/_error'
  66. end
  67. end
  68. end
  69. describe 'GET #show' do
  70. context 'the pokemon exists' do
  71. let!(:pokemon) { FactoryBot.create(:pokemon) }
  72. subject { get :show, params: {id: pokemon.id}, format: :json }
  73. it 'renders the pokemon JSON' do
  74. subject
  75. expect(response).to render_template 'api/v1/pokemon/show'
  76. end
  77. end
  78. end
  79. end