2009-02-26 4 views
6

Afin d'éviter d'ajouterRéglage de la HTTP_REFERER dans un monde avant (: tous) dans Rspec

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

à un avant bloc sur chaque fichier controller_spec je crée, j'ai essayé d'ajouter à la configuration globale (dans spec_helper.rb)

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

le problème est, je reçois l'erreur suivante:

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

quelqu'un at-il poin comment l'implémenter correctement?

À la votre!

Répondre

12

Avez-vous essayé

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

Cheers - Fonctionne parfaitement! – Codebeef

1

Je remarque que la réponse de Matt était il y a 2 ans, je ne suis pas sûr de la version « de rspec » qu'il utilisait. mais pour mon cas, ma version rspec = 1.3.2, et le segment de code ne fonctionne pas (toujours eu une erreur:

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

), jusqu'à ce que je change un peu:

# 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 

se référer à doc 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. 
Questions connexes