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

2019-04-26 17:39发布

问题:

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.

回答1:

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