Forum: Ruby on Rails scope problems testing a helper method that calls link_to()

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Rick B. (Guest)
on 2006-02-07 23:00
(Received via mailing list)
We've been having problems writing functional tests for helper methods
that rely on ActionView methods.  Here's a specific example.

In application_helper.rb I've got a method called 'party_link()ยจ:

  def party_link(party)
    link_to party.full_name, { :controller => 'contacts',
                               :action => 'view',
                               :category => party.category_name,
                               :id => party.id },
            :class => 'a2'
  end

It takes a Party object (person, organization...) and returns an HTML
link to
that party's contact page.  Simple enough.  And it appears to work well
enough
from the browser.

We want to test it, though, so we want to write a functional test that
fires
fixture Party data at it and ensure that it returns reasonable links,
for some
value of reasonable.

Since it's not in a particular controller's helper file (since it will
be used
by many controllers), we will create a trivial controller for use in
testing.
The problem we run into is that we can't seem to get the ActionView
methods
required to run party_link() visible while testing.  Here's the
functional
test:

----------------------------------------------------------------------
require File.dirname(__FILE__) + '/../test_helper'
require File.dirname(__FILE__) + '/../../app/controllers/application'
require File.dirname(__FILE__) + '/../../app/helpers/application_helper'
require 'action_view'

module LoginSystem
  def unprotected?() true; end # drop login protection for this test
controller
end

# dummy controller to drive helper tests
class HelperController < ActionController::Base
  include ApplicationHelper
  include ActionView
end

class HelperControllerTest < Test::Unit::TestCase
  fixtures :party

  include ApplicationHelper

  def setup
    @controller = HelperController.new
    @request    = ActionController::TestRequest.new
    @response   = ActionController::TestResponse.new
  end

  # Test party_link function passing staff
  def test_party_link_staff
    link_data = @controller.party_link(party(:twburges))
  end
end
----------------------------------------------------------------------

Note that we're not even getting to an assert_* in
test_party_link_staff(),
because, well, we tried, and we just get an error, so we culled the
assertions
from the code I'm sending (so as not to confuse the issue).

  1) Error:
test_party_link_staff(HelperControllerTest):
NoMethodError: undefined method `link_to' for
#<HelperController:0xb69e32d0>
    ./test/functional/../../app/helpers/application_helper.rb:170:in
`party_link'
    ./test/functional/bad_test.rb:30:in `test_party_link_staff'

Currently we're stubbing link_to() and checking that it gets called,
which is
The Wrong Way to fix the problem.  We can 'include' ActionView,
ActionView::Helpers, ActionView::Helpers::UrlHelper and get past link_to
(and,
I think url_for) but end up blowing on tag_options (IIRC).  Seems like
there
must be a simpler and more straightforward way to do this, that I'm sure
I
missed in Rails 101.

Any suggestions?
Rick
--
 http://www.rickbradley.com    MUPRN: 591
                       |  At present, one of
   random email haiku  |  the machines shows all the
                       |  detail from reboots.
This topic is locked and can not be replied to.