13

我目前正在研究用于生成 iPhone 特定 HTML 元标记的 Rails 插件。我正在尝试使用 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
4

3 回答 3

4

对我有用的可怕和hacky解决方法(因为我正在研究gem而不是在完整的rails环境中):

require 'ostruct'

module ActionController::UrlFor
  def _routes
    helpers = OpenStruct.new
    helpers.url_helpers = Module.new
    helpers
  end
end
于 2010-09-27T08:53:19.543 回答
1

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

include ActionDispatch::Routing::RouteSet

如果NameError提出 a 告诉您 ActionDispatch 未知,您可能必须这样做require 'action_dispatch'

于 2010-12-26T09:31:10.483 回答
0

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

于 2011-01-05T11:05:44.150 回答