Forum: Ruby on Rails Problems with testing - error posting to other 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.
Nick S. (Guest)
on 2005-12-14 22:00
Hi,

I'm trying to test my ad_controller. In order to create an ad, the user
has to be logged in, so I have written a login() that logs in a valid
user and generates a session[:user_id], like:

  def login (login="bob", password="atest")
    post "user/login", :user => { :login => login, :password => password
}

    assert_response 302  # redirect
    assert_session_has :user_id
    assert_equal 1000001, session[:user_id]

    assert_redirect_url "http://#{@request.host}/user/home"
  end

and added it to test_helper.rb . In my ad_controller_test I call it like
login(), but this throws an error. In my test.log it get the following

Processing AdController#user/login (for 0.0.0.0 at 2005-12-14 20:48:31)
[POST]
  Parameters: {"user"=>{"login"=>"bob", "password"=>"atest"},
"action"=>"user/login", "controller"=>"ad"}

It appears login goes to my ad_controller to search for a login method,
but there is none because it is defined in the user_controller. Is it
possible to do a post to another controller than the controller one is
testing? Or should I just instantiate a session and move on? Help is
very much appreciated. Thanks.

Kind regards,

Nick
Nick S. (Guest)
on 2005-12-15 12:37
Managed to get the login code to work. Had to add @controller =
UserController.new as the first line in the login method. And also had
to add @controller = AdController.new after I called login() in my
ad_controller_test. Is this the only solution or is there a nicer one?

Now that this is solved I'm having the following problem (next line in
test is giving problems :( ). I'm trying to do a post of some data in
two fixture files, like:

post :create, :ad => @ad_valid, :listing => @listing_ad_valid

@ad_valid and @listing_ad_valid are defined in the fixtures. But this
throws the error:

NoMethodError: undefined method `stringify_keys!' for #<Ad:0x3496d98>

I'm desperate. Testing should be fun!! All help is greatly appreciated.

Kind regards,

Nick

Nick S. wrote:
> Hi,
>
> I'm trying to test my ad_controller. In order to create an ad, the user
> has to be logged in, so I have written a login() that logs in a valid
> user and generates a session[:user_id], like:
>
>   def login (login="bob", password="atest")
>     post "user/login", :user => { :login => login, :password => password
> }
>
>     assert_response 302  # redirect
>     assert_session_has :user_id
>     assert_equal 1000001, session[:user_id]
>
>     assert_redirect_url "http://#{@request.host}/user/home"
>   end
>
> and added it to test_helper.rb . In my ad_controller_test I call it like
> login(), but this throws an error. In my test.log it get the following
>
> Processing AdController#user/login (for 0.0.0.0 at 2005-12-14 20:48:31)
> [POST]
>   Parameters: {"user"=>{"login"=>"bob", "password"=>"atest"},
> "action"=>"user/login", "controller"=>"ad"}
>
> It appears login goes to my ad_controller to search for a login method,
> but there is none because it is defined in the user_controller. Is it
> possible to do a post to another controller than the controller one is
> testing? Or should I just instantiate a session and move on? Help is
> very much appreciated. Thanks.
>
> Kind regards,
>
> Nick
This topic is locked and can not be replied to.