如何测试 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
调用总是会失败。 我尝试过使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
签出 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!
最终答案:
The final answer:
我得到的最接近的是
但我现在收到路由错误:
ActionController::RoutingError: No Route matches {:action=>"_renderizer", :controller=>"", :args=>{ :locals=>{:...}, :partial=>"/my_partial"}}
The closest I've gotten is
But I get a routing error now:
ActionController::RoutingError: No route matches {:action=>"_renderizer", :controller=>"", :args=>{:locals=>{:...}, :partial=>"/my_partial"}}
我建议您查看 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.