Forum: Ruby on Rails functional testing -- using post() w/ a different controller

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.
doug (Guest)
on 2005-11-18 13:06
Hallo -- I have a function in test_helper.rb that looks not a little
like this:

  def
login(username="gowondapiddlehead",unhashed_password="gowonda_piddlehead")
    post(:login,:system_user=>{:username=>username,:unhashed_password=>unhashed_password})
    assert_redirected_to(:controller=>'home')
    assert_not_nil(session[:system_user_id])
    system_user=SystemUser.find(session[:system_user_id])
    assert_equal(username,system_user.username,"Login name should match
session name.")
  end

When I run the test, however, I get the following error:

    1) Error:
  test_index(HomeControllerTest):
  NoMethodError: undefined method `login' for Test::Unit::TestCase:Class
      test/functional/home_controller_test.rb:18:in `test_index'

I guess this is because I need to specify the controller being called as
well as the action in the post() function.
Does anyone know how to do this?
Cheers,
   doug.
Zack (Guest)
on 2005-12-19 09:22
doug wrote:
> Hallo -- I have a function in test_helper.rb that looks not a little
> like this:
>
>   def
> login(username="gowondapiddlehead",unhashed_password="gowonda_piddlehead")
> 
post(:login,:system_user=>{:username=>username,:unhashed_password=>unhashed_password})
>     assert_redirected_to(:controller=>'home')
>     assert_not_nil(session[:system_user_id])
>     system_user=SystemUser.find(session[:system_user_id])
>     assert_equal(username,system_user.username,"Login name should match
> session name.")
>   end
>
> When I run the test, however, I get the following error:
>
>     1) Error:
>   test_index(HomeControllerTest):
>   NoMethodError: undefined method `login' for Test::Unit::TestCase:Class
>       test/functional/home_controller_test.rb:18:in `test_index'
>
> I guess this is because I need to specify the controller being called as
> well as the action in the post() function.
> Does anyone know how to do this?
> Cheers,
>    doug.

Doug,
  I'm having the exact same problem.  I have an account controller that
handles the login cycle. In testing another controller that depends on
this cycle I cannot specify the controller, only the action.
  The option to specify another controller should be added.
  Does anyone know a work-around?

Zack
Tom M. (Guest)
on 2005-12-19 10:01
(Received via mailing list)
On Dec 19, 2005, at 2:22 AM, Zack wrote:

>   I'm having the exact same problem.  I have an account controller
> that
> handles the login cycle. In testing another controller that depends on
> this cycle I cannot specify the controller, only the action.
>   The option to specify another controller should be added.
>   Does anyone know a work-around?

The functional test operators take a session argument, where you can set
the session up. Just test the pages by setting the session that
indicates
a login...

     get :show,             # <- method
         {  },              # <- params
         { :user_id => 4 }  # <- session

--
-- Tom M.
This topic is locked and can not be replied to.