如何在Rails中测试自定义助手?

xav*_*r23 3 ruby ruby-on-rails

如何测试生活在app/helpers/application_helper.rb中的帮助方法?

我在我的文件test/unit/helpers/application_helper_test.rb中有这段代码

require 'test_helper'

class ApplicationHelperTest < ActionView::TestCase


  test "category" do
    assert categories_collection
  end

end
Run Code Online (Sandbox Code Playgroud)

但我得到这个错误"NameError:undefined局部变量或方法`categories_collection'"

Joh*_*ley 5

试试这个:

require File.dirname(__FILE__) + '/../test_helper'
require 'application_helper'

class ApplicationHelperTest < Test::Unit::TestCase  
  include ApplicationHelper 

  def "category" do
    assert categories_collection 
  end 
end
Run Code Online (Sandbox Code Playgroud)