Testing components

I’m using template components to modularize an application I’m working
on and am really happy with the way it’s going. Unfortunately, all my
attempts at writing functional tests for them fail. Trying to duplicate
my normal controller tests I have:

require File.dirname(FILE) + ‘/…/test_helper’
require File.dirname(FILE) + ‘news_controller’

Re-raise errors caught by the controller.

class NewsController; def rescue_action(e) raise e end; end

class NewsControllerTest < Test::Unit::TestCase
def setup
@controller = NewsController.new
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
end

Replace this with your real tests.

def test_content
get :content
assert_response :success
assert_rendered_file “content”

end
end

But running that gives me:

  1. Error:
    test_content(NewsControllerTest):
    NoMethodError: undefined method `controller_path’ for
    NewsController:Class

/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/test_process.rb:295:in
`process’

/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/test_process.rb:307:in
get' news_controller_test.rb:16:intest_content’

So, I’m guessing I can’t use get() with components? Any advice?

Thanks,
Mike

Mike E. wrote:

I’m using template components to modularize an application I’m working
on and am really happy with the way it’s going. Unfortunately, all my
attempts at writing functional tests for them fail. Trying to
duplicate my normal controller tests I have:

require File.dirname(FILE) + ‘/…/test_helper’
require File.dirname(FILE) + ‘news_controller’

Errr… that second line should be:
require ‘news_controller’

Hi Mike

I’ve been trying the same thing - testing my Component, but receiving
errors all over - finally got it working, so here’s my finding(s):

  • The testfile goes in /components//test

  • The testfile is modified (basically prepending the componentname in
    various places:

require File.dirname(FILE) + ‘/…/…/test/test_helper’
require ‘/_controller’

Re-raise errors caught by the controller.

class ::Controller; def rescue_action(e)
raise e end; end

class ::ControllerTest <
Test::Unit::TestCase

@controller = ::Controller.new

Hope that helps,

/CS

Whoops:

require File.dirname(FILE) + ‘/…/…/test/test_helper’
require ‘/_controller’

require File.dirname(FILE) + ‘/…/…/…/test/test_helper’

  • need an extra up-level there to actually work :slight_smile:

/CS