【问题标题】:How can I test views in a Rails plugin?如何在 Rails 插件中测试视图?
【发布时间】:2010-09-20 23:29:39
【问题描述】:

我正在编写一个包含一些部分的 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 的通话总是失败。我尝试使用ActionView::Base 而不是ActionController::Base,但效果更差。

有人成功了吗?

【问题讨论】:

    标签: ruby-on-rails ruby testing plugins actionview


    【解决方案1】:

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

    您还可以使用这些来测试助手,我发现这非常有用。

    RSpec 也有测试视图的方法:http://rspec.info/documentation/rails/writing/views.html

    希望有帮助!

    【讨论】:

    • 无所事事。我尝试在 nil 上调用方法时遇到各种错误。好像需要做更多的设置。使用 ActionController::TestCase 并没有让它变得更好。
    【解决方案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
    

    【讨论】:

      【解决方案3】:

      我得到的最接近的是

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

      【讨论】:

        【解决方案4】:

        我建议您查看resource_controller plugin 的代码。我还在其他一些插件中看到了这种方法。

        答案很简单,在 test 目录中,您创建一个使用您的插件的应用程序。从那里,您只需使用常用工具来测试 Rails 视图。

        如果您的插件没有很多不同的用例,测试应用程序可以非常简单。对于更复杂的插件,例如 resource_controller,您可能需要创建很多不同的控制器等等。

        要诱使您的测试应用加载您的插件,最简单的方法是在测试应用的插件目录中创建一个指向 r_c 目录根目录的链接。这不适用于 Windows,仅适用于 POSIX 操作系统。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-10-31
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多