2010-10-27 10 views
19

मेरे पास एक दृश्य सहायक विधि है जो request.domain और request.port_string को देखकर एक यूआरएल उत्पन्न करती है।rspec सहायक परीक्षण के लिए अनुरोध ऑब्जेक्ट का नकल कैसे करें?

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

मैं इस विधि का परीक्षण rspec का उपयोग करना चाहता हूं।

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

लेकिन जब मैं rspec के साथ इस चलाने के लिए, मैं इस मिल:

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

किसी को भी कृपया मुझे यह पता लगाने की क्या मैं इसे ठीक करने के लिए क्या करना चाहिए मदद कर सकते हैं? मैं इस उदाहरण के लिए 'अनुरोध' ऑब्जेक्ट का नकल कैसे कर सकता हूं?

क्या यूआरएल उत्पन्न करने के कोई बेहतर तरीके हैं जहां सबडोमेन का उपयोग किया जाता है?

अग्रिम धन्यवाद।

उत्तर

21

आप सहायक 'के साथ सहायक विधि पहले जोड़ें करने के लिए है एक ही समस्या थी, मुझे यह समाधान काम करने के लिए मिला:

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

यह त्रुटि दे रहा है: अपवाद का सामना करना पड़ा: # shailesh

0

चेक बाहर Railscasts रेल 3 में उप डोमेन के बारे में स्क्रीनकास्ट: http://railscasts.com/episodes/221-subdomains-in-rails-3

यह मदद करनी चाहिए वे कैसे काम करने के विचार मिलता है और हो सकता है जिस तरह से आप उन सहायकों अपने आप को ऐसा करने के लिए कोशिश कर रहे हैं बदल जाते हैं।

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

साथ ही अलग अनुरोध विकल्प के लिए व्यवहार का परीक्षण करने के लिए, आप नियंत्रक सोचा अनुरोध वस्तु पहुंच सकता है::

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 
7

मैं

+0

मेरे लिए नियंत्रक में यह 'नियंत्रक के साथ काम किया तरह दिखता है। request.host = "http://test_my.com/" ' – AnkitG

9

यह एक नहीं है आपके प्रश्न का पूरा उत्तर, लेकिन रिकॉर्ड के लिए, आप ActionController::TestRequest.new() का उपयोग करके एक अनुरोध का नकल कर सकते हैं। कुछ ऐसा:

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

क्या आप विस्तृत कर सकते हैं? –

संबंधित मुद्दे