8

I am getting an uninitialized constant error when trying to include a helper module into a test.

I have the following files in my rails test directory

functional> admin> school_controller_test.rb
functional> controller_helper.rb

The class/modules bodies are as follows:

module ControllerHelper
  def check_sort_order (items, column, direction)
    ...
  end
end

class Admin::SchoolsControllerTest < ActionController::TestCase
  include ::ControllerHelper 

  test "should sort by columns" do
    check_sort_order(assigns(:schools), 'schools.name', 'asc')
    check_sort_order(assigns(:schools), 'schools.name', 'desc')
  end
end

When I run this, the test output is:

/.../.rvm/gems/ruby-1.9.2-p0/gems/rspec-core-2.3.0/lib/rspec/core/backward_compatibility.rb:20:in `const_missing': uninitialized constant ControllerHelper (NameError)

I've tried playing with the namespaces, but can't get the module mixed in at all! Any ideas why I'm getting this error? Or is this even the correct way to extract common test functions? I'm very new to Rails, so any advice would be appreciated :)

Cheers!

laura
  • 2,951
  • 9
  • 44
  • 61

2 Answers2

8

Try adding this to test_helper.rb:

require "test/functional/controller_helper"

Side note: Not sure about test:unit, but rspec has a spec/support directory for files to get auto-loaded.

Zubin
  • 9,422
  • 7
  • 48
  • 52
  • 1
    Thanks, nice one! I had to use require "functional/controller_helper" to get it to work with rake test, now all working perfectly. I'm going to start with rspec tomorrow, so i'll bear that in then - cheers!! – laura Dec 16 '10 at 12:12
  • Didn't know about spec/support. Nice! – clacke Jun 13 '11 at 09:10
0

For me what worked (found via trail and error) was:

require "./app/helpers/currencies_helper.rb"
stevec
  • 41,291
  • 27
  • 223
  • 311