2010-10-27 13 views
19

Ho un metodo di visualizzazione di vista che genera un URL esaminando request.domain e request.port_string.Come simulare l'oggetto richiesta per i test helper di rspec?

module ApplicationHelper 
     def root_with_subdomain(subdomain) 
      subdomain += "." unless subdomain.empty?  
      [subdomain, request.domain, request.port_string].join 
     end 
    end 

Vorrei testare questo metodo utilizzando rspec.

describe ApplicationHelper do 
    it "should prepend subdomain to host" do 
    root_with_subdomain("test").should = "test.xxxx:xxxx" 
    end 
end 

Ma quando ho eseguito questo con RSpec, ottengo questo:

Failure/Error: root_with_subdomain("test").should = "test.xxxx:xxxx" 
`undefined local variable or method `request' for #<RSpec::Core::ExampleGroup::Nested_3:0x98b668c>` 

Qualcuno può aiutarmi a capire cosa devo fare per risolvere questo problema? Come posso prendere in giro l'oggetto 'richiesta' per questo esempio?

Esistono modi migliori per generare URL in cui vengono utilizzati i sottodomini?

Grazie in anticipo.

risposta

21

si deve anteporre il metodo di supporto con 'aiutante':

describe ApplicationHelper do 
    it "should prepend subdomain to host" do 
    helper.root_with_subdomain("test").should = "test.xxxx:xxxx" 
    end 
end 

Inoltre per testare il comportamento per le diverse opzioni di richiesta, è possibile accedere l'oggetto della richiesta throught il controller:

describe ApplicationHelper do 
    it "should prepend subdomain to host" do 
    controller.request.host = 'www.domain.com' 
    helper.root_with_subdomain("test").should = "test.xxxx:xxxx" 
    end 
end 
+2

Sta dando errore: Eccezione riscontrata: # shailesh

7

I ha avuto un problema simile, ho trovato questa soluzione per lavorare:

before(:each) do 
    helper.request.host = "yourhostandorport" 
end 
+0

Per me regolatore ha funzionato con 'controller. request.host = "http://test_my.com/" ' – AnkitG

9

questa non è una completa la risposta alla tua domanda, ma per la cronaca puoi prendere in giro una richiesta usando ActionController::TestRequest.new(). Qualcosa del tipo:

describe ApplicationHelper do 
    it "should prepend subdomain to host" do 
    test_domain = 'xxxx:xxxx' 
    controller.request = ActionController::TestRequest.new(:host => test_domain) 
    helper.root_with_subdomain("test").should = "test.#{test_domain}" 
    end 
end 
+0

Potresti elaborare? –

Problemi correlati