您如何在Rails测试中测试params哈希? [英] How do you test the params hash in a Rails test?
问题描述
以下内容会产生错误:未定义的局部变量或方法'params'"
The following generates an error: "undefined local variable or method `params'"
assert_equal params[:recipient_id], users(:one).id
您如何测试params哈希?
How do you test the params hash?
此外,当存在参数时,如何测试assert_redirect
?参数已附加到URL,因此对model_path
或类似内容的测试失败.
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.
在Rails 3中使用内置的测试类.
Working with built in test class in Rails 3.
推荐答案
http://guides.rubyonrails.org/testing.html#functional-tests-for-your-controllers gives some of this information.
在这种情况下,params附加到@request
或@response
对象(取决于要测试的HTTP方法),因此可以将其称为@request.params[:recipient_id]
.
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]
.
对于重定向:
assert_redirected_to post_path(assigns(:post))
将断言您已重定向到给定模型的正确路径. Assigns方法应具有您在控制器内部设置的实例变量,以传递给视图
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
这篇关于您如何在Rails测试中测试params哈希?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!