14
我如何测试rescue_from是RSpec?我想确保如果出现其中一个例外情况,控制器正确设置闪存并执行重定向。有没有办法模拟异常?RSpec:测试rescue_from
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
更好的解决方案:https://www.relishapp.com/rspec/rspec-rails/docs/controller-specs/anonymous-controller – kaihowl 2012-02-18 01:31:23
我不明白stub ...对我来说,就像你让控制器在测试中提出错误一样。如果你只是在测试中伪造它,你怎么能确定它会提高生产中的错误? – hamstar 2014-01-29 03:14:22
@hamstar这只测试'rescue_from'块的行为。为了确保'authorize!'产生错误,你需要为此写另一个测试。据推测,这发生在一个图书馆(CanCan?),作者在那里写了测试。 – zetetic 2014-01-29 20:22:26