测试视图助手

发布于 2024-09-15 17:40:42 字数 777 浏览 7 评论 0原文

我目前正在开发一个 Rails 插件,用于生成 iPhone 特定的 HTML 元标记。我尝试使用 ActionView::TestCase 进行单元测试,但不断收到相同的错误。请参阅下面的文件内容和错误。任何想法或帮助将不胜感激。

test_helper.rb

require 'rubygems'
require 'test/unit'
require 'active_support'
require 'action_view'
require File.join(File.dirname(__FILE__), '..', 'lib', 'iphone_helper')

iphone_test_helper.rb

require 'test_helper'

class IphoneHelperTest < ActionView::TestCase
  test 'br' do
    tag = tag('br')
    assert_tag_in tag, '<br />'
  end
end

错误

RuntimeError: In order to use #url_for, you must include routing helpers explicitly. For instance, `include Rails.application.routes.url_helpers

I'm currently working on a Rails plugin used for generating iPhone specific HTML meta-tags. I'm trying to use ActionView::TestCase for unit tests but keep getting the same error. See file contents and error below. Any ideas or help would be much appreciated.

test_helper.rb

require 'rubygems'
require 'test/unit'
require 'active_support'
require 'action_view'
require File.join(File.dirname(__FILE__), '..', 'lib', 'iphone_helper')

iphone_test_helper.rb

require 'test_helper'

class IphoneHelperTest < ActionView::TestCase
  test 'br' do
    tag = tag('br')
    assert_tag_in tag, '<br />'
  end
end

error

RuntimeError: In order to use #url_for, you must include routing helpers explicitly. For instance, `include Rails.application.routes.url_helpers

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

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

发布评论

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

评论(3

゛时过境迁 2024-09-22 17:40:42

对我有用的糟糕而hacky的解决方法(因为我正在处理gem而不是在完整的rails环境中):

require 'ostruct'

module ActionController::UrlFor
  def _routes
    helpers = OpenStruct.new
    helpers.url_helpers = Module.new
    helpers
  end
end

Awful and hacky workaround that worked for me (since I am working on a gem and not in a full rails environment):

require 'ostruct'

module ActionController::UrlFor
  def _routes
    helpers = OpenStruct.new
    helpers.url_helpers = Module.new
    helpers
  end
end
狼性发作 2024-09-22 17:40:42

您是否尝试以老式方式包含相应模块?:

include ActionDispatch::Routing::RouteSet

如果 <引发 code>NameError 告诉您 ActionDispatch 未知,您可能必须require 'action_dispatch'

Did you try to include the respective Module in an old-fashioned way?:

include ActionDispatch::Routing::RouteSet

If a NameError is raised telling you that ActionDispatch is unknown you might have to require 'action_dispatch'.

海拔太高太耀眼 2024-09-22 17:40:42

也许是一个愚蠢的问题,但类名和文件名不匹配可能是一个问题(IphoneHelperTest 与 iphone_test_helper.rb)?有时这会导致类未加载。

Maybe a stupid question, but is the fact that the class name and the file name don't match possibly a problem (IphoneHelperTest vs. iphone_test_helper.rb)? Sometimes that leads to classes not being loaded.

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