测试视图助手

13 iphone unit-testing ruby-on-rails helpers

我目前正在研究用于生成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')
Run Code Online (Sandbox Code Playgroud)

iphone_test_helper.rb

require 'test_helper'

class IphoneHelperTest < ActionView::TestCase
  test 'br' do
    tag = tag('br')
    assert_tag_in tag, '<br />'
  end
end
Run Code Online (Sandbox Code Playgroud)

错误

RuntimeError: In order to use #url_for, you must include routing helpers explicitly. For instance, `include Rails.application.routes.url_helpers
Run Code Online (Sandbox Code Playgroud)

Mac*_*rio 4

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

require 'ostruct'

module ActionController::UrlFor
  def _routes
    helpers = OpenStruct.new
    helpers.url_helpers = Module.new
    helpers
  end
end
Run Code Online (Sandbox Code Playgroud)