Dan Bikle
2005-Nov-20 01:35 UTC
How I access controller instance variables from functional tests?
People, I noticed that instance variables in a controller are automatically available to corresponding templates in the view. Can any of you explain how this magic is implemented? What I''d like to do is work some more of this magic so I can access the instance variables from within functional tests. Thanks, -Dan
Marcel Molina Jr.
2005-Nov-20 01:48 UTC
Re: How I access controller instance variables from functional tests?
On Sat, Nov 19, 2005 at 05:35:36PM -0800, Dan Bikle wrote:> I noticed that instance variables in a controller > are automatically available to corresponding templates in the view. > > Can any of you explain how this magic is implemented?It''s in actionpack/lib/action_controller/base.rb add_variables_to_assigns And then in actionpack/lib/action_view/base.rb assign_variables_from_controller> What I''d like to do is work some more of this magic so I can access > the instance variables from within functional tests.In functional tests you can use the assigns method that is documented here: http://ap.rubyonrails.com/classes/Test/Unit/Assertions.html In your controller: class FooController < ApplicationController def bar @baz = ''...'' end end In your functional test: def test_bar assert_equal ''...'', assigns(:baz) end marcel -- Marcel Molina Jr. <marcel-WRrfy3IlpWYdnm+yROfE0A@public.gmane.org>
Peter Donald
2005-Nov-20 01:54 UTC
Re: How I access controller instance variables from functional tests?
Hi, The instance variables are already available to your functional tests via the assigns method which returns a hash of all instance variables (ie :symbol => value). For example assigns(:comment).description accesses the description field of the comment instance variable ''@comment'' from within my functional test after I have done the get/post. HTH, On 11/20/05, Dan Bikle <dan.bikle-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> People, > > I noticed that instance variables in a controller > are automatically available to corresponding templates in the view. > > Can any of you explain how this magic is implemented? > > What I''d like to do is work some more of this magic so I can access > the instance variables from within functional tests.-- Cheers, Peter Donald RealityForge.org