Topic: Testing controller

Hi all!

I am learning test driven development and am a little stuck regarding controllers. I am setting a @variable depending on different stuff - eg. to "yes" if it is before 4am and to "no" if it is after (just an example).

How do I test that? Assert @variable=="yes" doesn't work, the variable isn't defined.

Regards,
Jacob Riff

Re: Testing controller

The tests are not in the right scope to access the controller's instance variables. If you look in the setup method, you'll see that it creates a controller object. That is why @variable won't work as you expected.

To access them, you can call assigns()
assert !assigns(:variable_name).nil?
assert_equal assigns(:variable_name), 'yes'

Vincent Woo Ruby on Rails Blog

Re: Testing controller

Hi,

  Friend I am using assert_tag for testing ROR applications I am doing following things:

assert_tag :tag => "div", :attributes => { :id => "hello" }

And I am getting following error.... Can u plz help me ...

1) Error:

test_assert_tag(AccountControllerTest):

RuntimeError: expected > (got ".length; i++) {\t\t\t\n\t\t\t\t//if the element is selected \n\t\t\t\tif (elementList[i].id == selected.id){\n\t\t\t\t\tselected.className = selectedCssName;\n\t\t\t\t}\n\t\t\t\telse{ //if the element is unselected\n\t\t\t\t\telementList[i].className = unselectedCssName;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\t" for <elementList.length; i++) {