Help needed using assigns() functionality in functional test


#1

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


#2

DANG - I posted this in the wrong place :-(. Sorry

Keith


#3

I think you want this instead:

assert_equal “howdy”, assigns(:string_segment)

Lance


#4

(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/


#5

On 1/20/06, Lance B. removed_email_address@domain.invalid 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.
:slight_smile:

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