6

追加を避けるために

request.env["HTTP_REFERER"] = '/'

作成するすべての controller_spec ファイルの before ブロックに、これをグローバル構成 (spec_helper.rb) に追加しようとしました

config.before(:each) {request.env["HTTP_REFERER"] = '/'}

問題は、次のエラーが表示されることです。

You have a nil object when you didn't expect it!
The error occurred while evaluating nil.env

これを正しく実装する方法についての指針はありますか?

乾杯!

4

2 に答える 2

12

やってみました

  config.before(:type => :controller) do
    request.env["HTTP_REFERER"] = "/"
  end
于 2009-02-26T20:08:39.493 に答える
1

Matt の回答は 2 年前のものであることに気付きました。彼が使用していた "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'
...

)、少し変更するまで:

# 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

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.
于 2011-07-10T23:27:03.437 に答える