views:

191

answers:

1

I have an method in ApplicationHelper

def javascript_auto_link_tags
  js_path = "#{RAILS_ROOT}/public/javascripts"
  js = "#{controller.controller_path}.js"
  javascript_include_tag(js) if FileTest.exist?("#{js_path}/#{js}")
end

This should find a javascript file based on the current controller file name and path so controllers/admin/blah_controller.rb would generate "/javascripts/admin/blah.js" and include it if it exists.

In RSpec I'm not sure of the best way to test this since I don't know how the controller is being treated application_helper_spec.rb.

My current test is:

it "should load controller specific js" do
  FileTest.should_receive(:exist?).and_return(true)
  helper.javascript_auto_link_tags.should include("/javascripts/spec/rails/example/helper_example_group.js")
end

I basically pulled the result by checking for nil, but I don't know if relying on how RSpec treats the controller_path (must be "/spec/rails/example/helper_example_group_controller").

'ApplicationHelper for javascript auto link tags should load controller specific css' FAILED expected nil, got "" ./spec/helpers/application_helper_spec.rb:34:

Is there a better way to explicitly set the controller and path name?

A: 
  controller.stub!(:controller_path).and_return("foo")
Perryn Fowler