Forum: Rails Engines Help needed using assigns() functionality in functional test

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
D4e51fd9554030ab55c379fdc1a34826?d=identicon&s=25 Keith Lancaster (klancaster)
on 2006-01-20 20:14
I'm trying to create a functional test where the controller action (for
now, anyway) breaks apart a string and populates intermediate variables
with the results. I want to check the intermediate vars in my test, so I
tried the following

(in the controller)

def string_action
  # do some processing, etc.
  @string_segment = "howdy"
end

(in the unit test)
def test_the_string_doohicky
  post :string_action, ... some params here, etc.
  assert_equal "howdy", assigns["string_segment"]
end

The assigns[] always comes up nil, so I must be missing something here.
Any pointers?

TIA,
Keith
D4e51fd9554030ab55c379fdc1a34826?d=identicon&s=25 Keith Lancaster (klancaster)
on 2006-01-20 21:38
DANG - I posted this in the wrong place :-(. Sorry

Keith
D046cca1a33655b6285065ec89711389?d=identicon&s=25 Lance Ball (Guest)
on 2006-01-20 22:10
(Received via mailing list)
I think you want this instead:

assert_equal "howdy", assigns(:string_segment)

Lance
78d45b303250a077fc65fd61e00277ed?d=identicon&s=25 Jon Lim (Guest)
on 2006-01-20 22:19
(Received via mailing list)
> (in the unit test)
> def test_the_string_doohicky
>   post :string_action, ... some params here, etc.
>   assert_equal "howdy", assigns["string_segment"]
> end

What happens if you use
assigns(:string_segment)
instead?

--
http://www.snowblink.co.uk/
D046cca1a33655b6285065ec89711389?d=identicon&s=25 Lance Ball (Guest)
on 2006-01-20 22:19
(Received via mailing list)
On 1/20/06, Lance Ball <lanceball@gmail.com> wrote:
>
> I think you want this instead:
>
> assert_equal "howdy", assigns(:string_segment)
>
> Lance


Bad form to follow up my own post, but I'll elaborate.

You don't necessarily have to use a symbol, you could use the
"string_segment" string if you want, but all of the cool kids use
symbols.
:-)

I am not sure when the assigns hash changed to a method call, but the
info
in this doc seems to be outdated (
http://manuals.rubyonrails.com/read/chapter/28#page233).  If you look at
the
rails code, action_controller/test_process.rb has this method:

module Test
  module Unit
    class TestCase #:nodoc:
# ...
       def assigns(key = nil)
          if key.nil?
            @response.template.assigns
          else
            @response.template.assigns[key.to_s]
          end
        end
# ...
end

So it appears that the hash itself has moved into @response.template and
that you're calling this method when you are testing for assignments in
your
controller tests.

Lance
This topic is locked and can not be replied to.