gpt4 book ai didi

testing - RSpec:测试 rescue_from

转载 作者:行者123 更新时间:2023-11-28 19:40:56 26 4
gpt4 key购买 nike

如何测试 rescue_from 是 RSpec?我想确保如果出现其中一个异常, Controller 会正确设置闪光灯并进行重定向。有没有办法模拟异常?

  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

最佳答案

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

  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

关于testing - RSpec:测试 rescue_from,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4466399/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com