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

风流意气都作罢 提交于 2019-12-07 02:34:01

问题


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



来源:https://stackoverflow.com/questions/6628021/how-do-you-test-the-params-hash-in-a-rails-test

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!