How can I test rescue_from is RSpec? I'd like to make sure that if one of the exceptions is raised, that the controller correctly sets the flash and does the redirect. Is there a way to simulate the exception?
rescue_from PageAccessDenied do
flash[:alert] = "You do not have the necessary roles to access this page"
redirect_to root_url
end
rescue_from CanCan::AccessDenied do |exception|
flash[:alert] = exception.message
redirect_to root_url
end
Assuming that you have an authorize!
method that raises the exception, you should be able to do something like this:
describe "rescue_from exceptions" do
it "rescues from PageAccessDenied" do
controller.stub(:authorize!) { raise PageAccessDenied }
get :index
response.should redirect_to("/")
flash[:alert].should == "You do not have the necessary roles to access this page"
end
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With