質問

一部のパーシャルを含む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 を使用しようとしましたが、それはさらに遠くなります。

成功した人はいますか

役に立ちましたか?

解決 2

最終回答:

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

他のヒント

checkout ActionView :: TestCase- http://api.rubyonrails.org/classes /ActionView/TestCase.html

これらを使用してヘルパーをテストすることもできますが、これは非常に役立ちました。

RSpecにはビューをテストする方法もあります: http://rspec.info/ documentation / rails / writing / views.html

役立つことを願っています!

私が得た最も近いものは

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 =&gt;&quot; _renderizer&quot ;,:controller =&gt;&quot;&quot ;,:args =&gt; {: locals =&gt; {:...}、:partial =&gt;&quot; / my_partial&quot;}}

resource_controllerプラグインのコードをご覧になることをお勧めします。他のいくつかのプラグインでもこのアプローチを見てきました。

答えは簡単です。 test ディレクトリで、プラグインを使用するアプリを作成します。そこから、一般的なツールを使用してRailsビューをテストします。

プラグインの使用例が多くない場合、テストアプリは非常にシンプルになります。 resource_controllerのようなより複雑なプラグインの場合、おそらくかなりの数の異なるコントローラーなどを作成する必要があります。

テストアプリをだましてプラグインをロードするには、テストアプリのプラグインディレクトリ内にr_cディレクトリのルートへのリンクを作成するのが最も簡単な方法です。これはWindowsでは動作せず、POSIX OSでのみ動作します。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top