Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RSpec: Testing rescue_from

Tags:

testing

rspec

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
like image 689
Eric M. Avatar asked Dec 16 '10 23:12

Eric M.


1 Answers

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
like image 76
zetetic Avatar answered Nov 05 '22 11:11

zetetic