首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >RSpec:测试rescue_from

RSpec:测试rescue_from
EN

Stack Overflow用户
提问于 2010-12-17 07:35:31
回答 1查看 8.5K关注 0票数 17

如何测试rescue_from is RSpec?我希望确保如果引发其中一个异常,控制器将正确设置闪存并执行重定向。有没有办法模拟异常?

代码语言:javascript
复制
  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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-12-17 12:58:38

假设您有一个引发异常的authorize!方法,您应该能够执行以下操作:

代码语言:javascript
复制
  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
票数 14
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4466399

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档