如何测试 Rails 插件中的视图?

发布于 2024-07-07 17:52:46 字数 661 浏览 11 评论 0原文

我正在编写一个 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,但效果更差。

有人取得过成功吗?

I'm writing a Rails plugin that includes some partials. I'd like to test the partials, but I'm having a hard time setting up a test that will render them. There's no associated controller, so I'm just faking one:

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

But that :render call always blows up. I've tried using an ActionView::Base instead of an ActionController::Base, but that gets even less far.

Has anyone had any success?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

°如果伤别离去 2024-07-14 17:52:46

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

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

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

希望有帮助!

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

You can also use these to test helpers, which I've found extremely helpful.

RSpec also has a way to test views: http://rspec.info/documentation/rails/writing/views.html

Hope that helps!

北凤男飞 2024-07-14 17:52:46

最终答案:

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

The final answer:

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
ㄟ。诗瑗 2024-07-14 17:52:46

我得到的最接近的是

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"}}

The closest I've gotten is

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

But I get a routing error now: ActionController::RoutingError: No route matches {:action=>"_renderizer", :controller=>"", :args=>{:locals=>{:...}, :partial=>"/my_partial"}}

╭ゆ眷念 2024-07-14 17:52:46

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

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

如果您的插件没有很多不同的用例,那么测试应用程序可能会非常简单。 对于更复杂的插件,例如resource_controller,您可能必须创建相当多不同的控制器等等。

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

I suggest you look at the code for the resource_controller plugin. I've also seen the approach in a few other plugins.

The answer is simple, in the test directory, you create an app that uses your plugin. From there, you simply use the common tools to test Rails views.

The test app can be extremely simple if there aren't many different use cases for your plugin. In the case of more complex plugins, like resource_controller, you'll probably have to create quite a few different controllers and so on.

To trick your test app into loading your plugin, the simplest way is to create a link to the root of the r_c directory inside the test app's plugin directory. This isn't gonna work on Windows, only POSIX OSes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文