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. Thanks in advance -- Posted via http://www.ruby-forum.com/. -- 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-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Aug 9, 8:14 pm, Jim Burgess <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> 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? >it''s an instance of the controller class that you''re testing (ActionController::TestCase sets one up for you) Fred -- 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-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Hi Jim, On Mon, Aug 9, 2010 at 2:14 PM, Jim Burgess <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> 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?See Section 4.4 Instance Variables in http://guides.rubyonrails.org/testing.html HTH, Bill -- 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-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 9 August 2010 20:14, Jim Burgess <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Hi, Thanks for all of the replies. That has really made things a lot clearer for me. So, to summarize: ''url_for'' is a method defined in ActionController::Base. @controller is an instance variable initiated by ActionController::TestCase containing a reference to my controller (PagesController). PagesController inherits from ApplicationController. ApplicationController inherits from ActionController::Base. Therefore, in ActionController::TestCase, by prepending a method call with @controller, owing to the aforementioned class hierarchy, I can access methods defined in ActionController::Base. Would that be correct? Thanks again for your help. -- Posted via http://www.ruby-forum.com/. -- 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-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Aug 9, 9:08 pm, Jim Burgess <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hi, > > Thanks for all of the replies. > That has really made things a lot clearer for me. > > So, to summarize: > > ''url_for'' is a method defined in ActionController::Base. > > @controller is an instance variable initiated by > ActionController::TestCase containing a reference to my controller > (PagesController). > > PagesController inherits from ApplicationController. > > ApplicationController inherits from ActionController::Base. > > Therefore, in ActionController::TestCase, by prepending a method call > with @controller, owing to the aforementioned class hierarchy, I can > access methods defined in ActionController::Base.Some of the way you phrased that seems a bit awkward to me. It not that @controller contains a reference to you controller - it is an actual instance of it. And ''prepending a method call'' sounds like you think there is some magic going on - there isn''t - methods are always called on a certain object (omitting the object means the method is called on self) Fred> > Would that be correct? > > Thanks again for your help. > > -- > Posted viahttp://www.ruby-forum.com/.-- 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-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
> It not that @controller contains a reference to you controller - > it is an actual instance of it.OK, I phrased that badly.> And ''prepending a method call'' sounds like you think there is some > magic going on - there isn''t - methods are always called on a certain > object (omitting the object means the method is called on self)And that too :-) Thanks for clearing that up. -- Posted via http://www.ruby-forum.com/. -- 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-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.