On 9 August 2010 20:14, Jim Burgess <li...@ruby-forum.com> wrote:
> In a book I'm reading the author tries to do the following from within
> his controller test file:
>
> class PagesControllerTest < ActionController::TestCase
>  def test_create_link_in_index
>    get :index
>    assert_select "a[href=?]", url_for(:action => :new), :text => 'Neue
> Seite'
>  end
> end
>
> However the method 'url_for' is unknown within the class
> PagesControllerTest, as it is defined in ActionController::Base.
>
> To get around this he writes (although this is not the final solution):
>
> assert_select "a[href=?]", @controller.url_for(:action => :new,
> :only_path => true), :text => 'Neue Seite'
>
> How is he able to reference a method defined in another class, simply by
> prefixing the method call with '@controller'
>
> Could someone explain what is '@controller' in this context?
>
> I tried Googling it, but as Google ignores the '@' I couldn't get very
> far.

'@controller' is an instance variable in ActionController::TestCase.
ActionController::TestCase sets up this variable to contain an
instance of the controller class you are testing (in this case,
PagesController).

In other words, ActionController::TestCase does something like this:

@controller = PagesController.new

which is why you're able to call PagesController methods on @controller.

(The '@' symbol before a variable in Ruby just means that it is an
instance variable, as opposed to an ordinary local variable.)

Chris

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-t...@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to