2010-10-27 11 views
19

Mam metodę viewer, która generuje adres URL, patrząc na request.domain i request.port_string.Jak wyśmiać obiekt żądania dla testów pomocnika rspec?

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

Chciałbym przetestować tę metodę za pomocą rspec.

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

Ale gdy uruchamiam to z RSpec, otrzymuję to:

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

Czy ktoś może mi pomóc dowiedzieć się, co należy zrobić, aby rozwiązać ten problem? Jak mogę sfałszować obiekt "request" dla tego przykładu?

Czy istnieją lepsze sposoby generowania adresów URL, w których używane są subdomeny?

Z góry dziękuję.

Odpowiedz

21

trzeba poprzedzić metody pomocnika z „pomocnik”:

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

Dodatkowo do badania zachowań dla różnych opcji żądanie, można uzyskać dostęp do obiektu żądania throught regulatora:

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

Daje błąd: Wystąpił wyjątek: # shailesh

7

I miał podobny problem, znalazłem to rozwiązanie do pracy:

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

dla mnie w sterowniku to działało z 'kontrolera. request.host = "http://test_my.com/" ' – AnkitG

9

To nie jest kompletna odpowiedź na twoje pytanie, ale dla przypomnienia możesz wysmakować prośbę używając ActionController::TestRequest.new(). Coś takiego:

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

Czy mógłbyś rozwinąć? –

Powiązane problemy