Andrew Swistak 6 gadi atpakaļ
vecāks
revīzija
cfd97175a9

+ 35 - 7
spec/controllers/pokemon_controller_spec.rb → spec/controllers/api/v1/pokemon_controller_spec.rb

@@ -1,6 +1,14 @@
+# frozen_string_literal: true
+
 require 'rails_helper'
 
-RSpec.describe PokemonController, type: :controller do
+RSpec.describe API::V1::PokemonController, type: :controller do
+  def non_existant_pokemon_id(initial_id = 1)
+    id = initial_id
+    id *= 2 until !Pokemon.find_by(id: id)
+    id
+  end
+
   describe "DELETE #destroy" do
     let!(:pokemon) { FactoryBot.create(:pokemon) }
     subject { delete :destroy, params: {id: pokemon.id} }
@@ -8,7 +16,7 @@ RSpec.describe PokemonController, type: :controller do
     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
+        expect(response).to render_template 'api/v1/pokemon/show'
       end
     end
 
@@ -17,7 +25,16 @@ RSpec.describe PokemonController, type: :controller do
 
       it 'does not delete the pokemon' do
         expect{subject}.to change{Pokemon.count}.by(0)
-        expect(response).to render_template :show
+        expect(response).to render_template 'api/v1/application/_error'
+      end
+    end
+
+    context "the pokemon does not exist" do
+      let(:pokemon) { double(id: non_existant_pokemon_id ) }
+
+      it 'raises an error' do
+        expect{subject rescue nil}.to change{Pokemon.count}.by(0)
+        expect(response).to render_template 'api/v1/application/_error'
       end
     end
   end
@@ -38,7 +55,7 @@ RSpec.describe PokemonController, type: :controller do
         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
+          expect(response).to render_template 'api/v1/application/_error'
         end
       end
 
@@ -49,7 +66,7 @@ RSpec.describe PokemonController, type: :controller do
 
         it 'creates some new pokemon' do
           expect{subject}.to change{Pokemon.count}.by(1)
-          expect(response).to redirect_to pokemon_index_path
+          expect(response).to render_template 'api/v1/pokemon/index'
         end
       end
 
@@ -62,8 +79,19 @@ RSpec.describe PokemonController, type: :controller do
 
       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
+        expect(response).to render_template 'api/v1/application/_error'
+      end
+    end
+  end
+
+  describe 'GET #show' do
+    context 'the pokemon exists' do
+      let!(:pokemon) { FactoryBot.create(:pokemon) }
+      subject { get :show, params: {id: pokemon.id}, format: :json }
+
+      it 'renders the pokemon JSON' do
+        subject
+        expect(response).to render_template 'api/v1/pokemon/show'
       end
     end
   end

+ 1 - 1
spec/factories/pokemon.rb → spec/factories/pokemon_factory.rb

@@ -1,6 +1,6 @@
 FactoryBot.define do
   factory :pokemon do
     pokedex_number { Random.rand(151) }
-    nickname { Faker::Pokemon.name }
+    nickname { Faker::Games::Pokemon.name }
   end
 end

+ 6 - 0
spec/lib/api_error/base_error_spec.rb

@@ -0,0 +1,6 @@
+# frozen_string_literal: true
+
+require 'rails_helper'
+
+RSpec.describe APIError::BaseError, type: :lib do
+end