2008-11-05 8 views
5

मैं एक रेल प्लगइन लिख रहा हूं जिसमें कुछ आंशिक शामिल हैं। मैं आंशिक परीक्षण करना चाहता हूं, लेकिन मुझे एक परीक्षण स्थापित करने में कठिनाई हो रही है जो उन्हें प्रस्तुत करेगी। कोई जुड़े नियंत्रक है, तो मैं सिर्फ एक faking हूँ:मैं रेल प्लगइन में विचारों का परीक्षण कैसे कर सकता हूं?

require 'action_controller' 
require 'active_support' 
require 'action_pack' 
require 'action_view' 

class MyTest < Test::Unit::TestCase 
    def setup 
    @renderer = ActionController::Base.new 
    @renderer.append_view_path File.expand_path(File.join(File.dirname(__FILE__), '..', 'views')) 
    end 

    def test_renders_link 
    result = @renderer.render(:partial => '/something') 
    assert ... 
    end 
end 

लेकिन उस :render कॉल हमेशा ऊपर चल रही है। मैंने ActionController::Base के बजाय ActionView::Base का उपयोग करने का प्रयास किया है, लेकिन यह भी कम हो जाता है।

क्या किसी को भी कोई सफलता मिली है?

उत्तर

1

अंतिम जवाब:

require 'action_controller' 
require 'active_support' 
require 'action_pack' 
require 'action_view' 
require 'action_controller/test_case' 

class StubController < ActionController::Base 
    helper MyHelper 
    append_view_path '...' 
    attr_accessor :thing 
    def my_partial 
    render :partial => '/my_partial', :locals => { :thing => thing } 
    end 
    def rescue_action(e) raise e end; 
end 

class MyTestCase < ActionController::TestCase 
    self.controller_class = StubController 
    def setup 
    @controller.thing = ... 
    get :my_partial 
    assert ... 
    end 
end 
2

चेकआउट ActionView :: testcase - http://api.rubyonrails.org/classes/ActionView/TestCase.html

तुम भी सहायकों, जो मैं अत्यंत उपयोगी पाया है परीक्षण करने के लिए इन का उपयोग कर सकते हैं। http://rspec.info/documentation/rails/writing/views.html

आशा है कि मदद करता है:

RSpec भी एक तरह से देखा गया परीक्षण करने के लिए है!

+0

नोथिन 'कर रहा है। मुझे शून्य पर विधियों को कॉल करने की कोशिश कर रहे सभी प्रकार की त्रुटियां मिलती हैं। ऐसा लगता है कि अधिक सेटअप करने की जरूरत है। ActionController का उपयोग :: टेस्टकेस इसे बेहतर नहीं बनाता है। –

0

निकटतम मैं मिल गया है

require 'action_controller' 
require 'active_support' 
require 'action_pack' 
require 'action_view' 
require 'action_controller/test_case' 

class StubController < ActionController::Base 
    append_view_path '...' 
    def _renderizer; render params[:args]; end 
    def rescue_action(e) raise e end; 
end 

class MyTest < ActionController::TestCase 
    self.controller_class = StubController 
    def render(args); get :_renderizer, :args => args; end 
    def test_xxx 
    render :partial => ... 
    end 
end 

है लेकिन मैं एक मार्ग त्रुटि अब मिलती है: ActionController::RoutingError: No route matches {:action=>"_renderizer", :controller=>"", :args=>{:locals=>{:...}, :partial=>"/my_partial"}}

0

मैं सुझाव है कि आप के लिए कोड को देखो resource_controller plugin। मैंने कुछ अन्य प्लगइन में दृष्टिकोण भी देखा है।

उत्तर परीक्षण निर्देशिका में उत्तर सरल है, आप एक ऐप बनाते हैं जो आपकी प्लगइन का उपयोग करता है। वहां से, आप बस रेल विचारों का परीक्षण करने के लिए सामान्य उपकरण का उपयोग करते हैं।

यदि आपके प्लगइन के लिए कई अलग-अलग उपयोग मामले नहीं हैं तो परीक्षण ऐप बेहद सरल हो सकता है। अधिक जटिल प्लगइन्स के मामले में, resource_controller की तरह, आपको शायद कुछ अलग नियंत्रक बनाना होगा और इसी तरह।

अपनी प्लगइन को लोड करने में अपने परीक्षण ऐप को चालित करने के लिए, सबसे आसान तरीका परीक्षण ऐप की प्लगइन निर्देशिका के अंदर r_c निर्देशिका की रूट को लिंक बनाना है। यह विंडोज़ पर काम नहीं करेगा, केवल पॉज़िक्स ओएस।

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

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