2009-02-26 10 views
6

Al fine di evitare di aggiungereImpostazione del HTTP_REFERER in un globale prima (: all) in Rspec

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

a una prima del blocco su ogni file controller_spec creo, ho cercato di aggiungere questo alla configurazione globale (in spec_helper.rb)

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

il problema è, ottengo il seguente errore:

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

qualcuno ha poin su come implementarlo correttamente?

Cheers!

risposta

12

Hai provato

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

Saluti - Funziona perfettamente! – Codebeef

1

Ho notato che la risposta di Matt era 2 anni fa, io non sono sicuro di quale versione "rspec" che stava usando. ma per il mio caso, la mia versione RSpec = 1.3.2, e il segmento di codice non funziona (sempre ottenuto un errore:

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

), fino a quando lo cambio un po ':

# 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 

fare riferimento al documento di 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. 
Problemi correlati