diff --git a/app/controllers/likes_controller.rb b/app/controllers/likes_controller.rb index 8822af378..adf92827e 100644 --- a/app/controllers/likes_controller.rb +++ b/app/controllers/likes_controller.rb @@ -15,7 +15,7 @@ class LikesController < ApplicationController @like = if target current_user.like!(target) end - rescue ActiveRecord::RecordInvalid => e + rescue ActiveRecord::RecordNotFound, ActiveRecord::RecordInvalid => e # do nothing end diff --git a/spec/controllers/likes_controller_spec.rb b/spec/controllers/likes_controller_spec.rb index 7cc8d3d39..406116a1c 100644 --- a/spec/controllers/likes_controller_spec.rb +++ b/spec/controllers/likes_controller_spec.rb @@ -72,6 +72,22 @@ describe LikesController, :type => :controller do expect(response.code).to eq('422') end end + + context "when an the exception is raised" do + it "should be catched when it means that the target is not found" do + allow(@target).to receive(:id).and_return(-1) + post :create, like_hash.merge(:format => :json) + expect(response.code).to eq('422') + end + + it "should not be catched when it is unexpected" do + @target = alice.post :status_message, :text => "AWESOME", :to => @alices_aspect.id + @target = alice.comment!(@target, "hey") if class_const == Comment + allow(alice).to receive(:like!).and_raise("something") + allow(@controller).to receive(:current_user).and_return(alice) + expect { post :create, like_hash.merge(:format => :json) }.to raise_error("something") + end + end end describe '#index' do