2008-11-05 3 views
5

일부 partials를 포함하는 Rails 플러그인을 작성하고 있습니다. 부분품을 테스트하고 싶지만, 테스트를 설정하는 데 어려움을 겪고 있습니다. 아무 관련 컨트롤러에 없다, 그래서 난 그냥 하나를 꾀하고 있습니다 :Rails 플러그인에서 뷰를 테스트하려면 어떻게해야합니까?

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

Nothin '하고 있습니다. 나는 모든 종류의 에러를 발생시키지 않고 메소드를 호출하려고한다. 마치 더 많은 설정이 필요합니다. ActionController :: TestCase를 사용하면 더 나아지지 않습니다. –

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 디렉토리의 루트에 대한 링크를 만드는 것입니다. 이것은 Windows에서는 작동하지 않고 POSIX OS에서만 작동합니다.

관련 문제