2009-02-26 17 views
6

Con el fin de evitar la adición deAjuste del HTTP_REFERER en un global antes (: all) en Rspec

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

a una antes del bloque en cada fichero controller_spec creo, me han tratado de añadir esto a la configuración global (en spec_helper.rb)

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

el problema es que me sale el siguiente error:

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

alguien alguna poin ters sobre cómo implementar esto correctamente?

¡Salud!

Respuesta

12

Ha intentado

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

Cheers - ¡Funciona perfectamente! – Codebeef

1

Me he dado cuenta de que la respuesta de Matt fue hace 2 años, no estoy seguro de lo que "rspec" la versión que estaba usando. pero para mi caso, mi versión rspec = 1.3.2, y el segmento de código no funciona (siempre tiene un error:

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' 
... 

), hasta que cambie un poco:

# 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 

consulte el documento de 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. 
Cuestiones relacionadas