Ruby on rails 在Rspec中的全局before(:all)中设置HTTP_REFERER

Ruby on rails 在Rspec中的全局before(:all)中设置HTTP_REFERER,ruby-on-rails,rspec,Ruby On Rails,Rspec,为了避免添加 request.env["HTTP_REFERER"] = '/' 对于我创建的每个controller\u spec文件上的before块,我尝试将其添加到全局配置(在spec\u helper.rb中) 问题是,我得到以下错误: You have a nil object when you didn't expect it! The error occurred while evaluating nil.env 有人对如何正确实现这一点有任何建议吗 干杯 你试过了吗 c

为了避免添加

request.env["HTTP_REFERER"] = '/'
对于我创建的每个controller\u spec文件上的before块,我尝试将其添加到全局配置(在spec\u helper.rb中)

问题是,我得到以下错误:

You have a nil object when you didn't expect it!
The error occurred while evaluating nil.env
有人对如何正确实现这一点有任何建议吗

干杯

你试过了吗

  config.before(:type => :controller) do
    request.env["HTTP_REFERER"] = "/"
  end

我注意到Matt的答案是两年前的,我不确定他使用的是什么“rspec”版本。 但对于我的情况,我的rspec版本=1.3.2,代码段不工作(总是出错:

You might have expected an instance of Array.
The error occurred while evaluating nil.<<
    from /usr/lib/ruby/gems/1.8/gems/rspec-1.3.2/lib/spec/runner/configuration.rb:181:in `__send__'
    from /usr/lib/ruby/gems/1.8/gems/rspec-1.3.2/lib/spec/runner/configuration.rb:181:in `add_callback'
    from /usr/lib/ruby/gems/1.8/gems/rspec-1.3.2/lib/spec/runner/configuration.rb:101:in `before'
...
参考rspec-1.3.2的文件:

append_before(scope = :each, options={}, &proc)
Appends a global before block to all example groups. scope can be any of 
:each (default), :all, or :suite. 
When :each, the block is executed before each example. 
When :all, the block is executed once per example group, before any of its examples are run. 
When :suite the block is run once before the entire suite is run.
# here the ":each" symbol is very important and necessary.  
# :type => :controller is the "option"
config.before(:each, :type => :controller) do
  request.env["HTTP_REFERER"] = "/"
end
append_before(scope = :each, options={}, &proc)
Appends a global before block to all example groups. scope can be any of 
:each (default), :all, or :suite. 
When :each, the block is executed before each example. 
When :all, the block is executed once per example group, before any of its examples are run. 
When :suite the block is run once before the entire suite is run.