Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you test the params hash in a Rails test?

The following generates an error: "undefined local variable or method `params'"

assert_equal params[:recipient_id], users(:one).id

How do you test the params hash?

Also, how do you test assert_redirect when there are params present? The params are appended to the URL, so testing for model_path or similar fails.

Working with built in test class in Rails 3.

like image 764
B Seven Avatar asked Jul 08 '11 17:07

B Seven


1 Answers

http://guides.rubyonrails.org/testing.html#functional-tests-for-your-controllers gives some of this information.

In this case, params is attached to the @request or @response object (depending on what HTTP method you are testing), so you can refer to it as @request.params[:recipient_id].

For redirect: assert_redirected_to post_path(assigns(:post)) will assert that you are redirected to the proper path for a given model. The assigns method should have the instance variables you are setting inside of the controller to pass to the view

like image 68
Jim Deville Avatar answered Sep 27 '22 22:09

Jim Deville