问题描述
rspec-rails (2.7.0) rails (3.0.10)帖子:Rails 3.1 错误捕获对我来说无关紧要.
rspec-rails (2.7.0) rails (3.0.10)post: Rails 3.1 Error Catching is irrelevant for me.
代码:
class ApplicationController < ActionController::Base
unless Rails.application.config.consider_all_requests_local
rescue_from ActiveRecord::RecordNotFound, :with => :render_404
rescue_from Exception, :with => :render_500
rescue_from FunnyException, :with => :my_errors
def my_errors(exception)
#some stuff for production
puts "=======From top====#{Rails.application.config.consider_all_requests_local}"
end
else
rescue_from FunnyException, :with => :my_errors
def my_errors(exception)
#some stuff for development
puts "=====From bottom======#{Rails.application.config.consider_all_requests_local}"
end
end
end
这段代码完美地适用于生产/开发......问题是我想用 rspec 测试它.如何在环境之间切换测试用例?
This code perfectly works with production/development... problem is when I wanna test it with rspec. How to switch test case between environments?
我尝试添加如下内容:
before do
Rails.application.config.consider_all_requests_local = true
end
我明白了:
...======从底部======true .======从底部======true .======从底部======真.======从底部======真
试过了
before do
Rails.application.config.consider_all_requests_local = false
end
所以我得到:
...======从底部======false .======从底部======false .======从底部======假.======从底部======假
这怎么可能?...与更改 spec_helper 相同
How is that even possible? ... same with changing spec_helper
config.before(:each) do
Rails.application.config.consider_all_requests_local = false
end
除非不工作.顶部部分无法使用 rspec... 总是点击 底部 为什么?我认为它现在与 Rack 以某种方式联系在一起......但是是否有任何解决方案可以在 rspec 中动态改变这种行为?
unless is not working. Top section is unreachable with rspec... always hits Bottom why ? I assume it is somehow tied now with Rack... but is there any solution to dynamically change this behavior inside rspec?
问候
推荐答案
好的,我找到了很简单的解决方案
Ok I found quite easy solution
before do
Rails.application.config.consider_all_requests_local = false
load "application_controller.rb"
end
after do
Rails.application.config.consider_all_requests_local = true
load "application_controller.rb"
end
它是匿名应用程序控制器测试套件的一部分.
It is part of anonymous application controller test suite.
您必须在块之后添加...因为此更改将在其他套件中持续存在.
You have to add after block... because this change will persist through other suites.
欢迎任何改进:D
使用 spork 和 guard 有时会导致我出现随机错误......之前:似乎都解决了这个问题
edit: Using spork and guard causes for me sometimes random errors... before :all seems to solve that problem
这篇关于为什么考虑_all_requests_local 使用 rspec 配置失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!