# Testing components

**URL:** <https://www.ruby-forum.com/t/testing-components/99940>\
**Category:** Rails\
**Created:** [February 1, 2006, 10:39pm UTC](https://www.ruby-forum.com/t/testing-components/99940 "2006-02-01T22:39:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![evronm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f1d935/32.png) [@evronm](https://www.ruby-forum.com/u/evronm)\
**Post date:** [February 1, 2006, 10:39pm UTC](https://www.ruby-forum.com/t/testing-components/99940/1 "2006-02-01T22:39:20Z")

</div>

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:in`test\_content’

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

Thanks,  
Mike

---

<div class="post-metadata">

**Author:** ![evronm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f1d935/32.png) [@evronm](https://www.ruby-forum.com/u/evronm)\
**Post date:** [February 2, 2006, 12:50am UTC](https://www.ruby-forum.com/t/testing-components/99940/2 "2006-02-02T00:50:54Z")

</div>

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’

---

<div class="post-metadata">

**Author:** ![evronm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f1d935/32.png) [@evronm](https://www.ruby-forum.com/u/evronm)\
**Post date:** [February 21, 2006, 2:28am UTC](https://www.ruby-forum.com/t/testing-components/99940/3 "2006-02-21T02:28:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![evronm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f1d935/32.png) [@evronm](https://www.ruby-forum.com/u/evronm)\
**Post date:** [February 21, 2006, 8:42am UTC](https://www.ruby-forum.com/t/testing-components/99940/4 "2006-02-21T08:42:48Z")

</div>

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_ 🙂

/CS
