如何在 Rails 测试中指定 POST 参数?
使用 Test::Unit 和 Shoulda。 尝试测试Users.create
。 我的理解是 Rails 表单发送像这样的对象的参数:
user[email]
在你的操作中它会变成散列,对吗?
params[:user][:email]
好的,所以在我的测试中我已经尝试过......
setup { post :create, :post => { 'user[email]' => 'invalid@abc' } }
并且
setup { post :create, :post => { :user => { :email => 'abc@abcd' } } }
在这两种情况下,在我的操作中, params[:user]
为零。
Working with Test::Unit and Shoulda. Trying to test Users.create
. My understanding is that Rails forms send params for an object like this:
user[email]
Which turns into hash in your action, right?
params[:user][:email]
OK, so in my test I've tried...
setup { post :create, :post => { 'user[email]' => 'invalid@abc' } }
and
setup { post :create, :post => { :user => { :email => 'abc@abcd' } } }
In both cases, over in my action, params[:user]
is nil.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
get、post、put、delete 的所有测试方法的一般形式如下:
在测试中,params 哈希直接发送到控制器操作的
params
中,无需任何转换。 即使进行集成测试,您也确实不需要测试此字符串到参数的转换,因为它被 Rails 框架测试很好地覆盖了。 另外,所有需要参数的测试方法都以这种方式接受哈希,而不会抱怨,让事情变得简单。The general form for all the test methods of get, post, put, delete are as follows:
And in tests, the params hash gets directly sent into
params
of your controller action with no translation of any sort. Even doing integration testing you really shouldnt need to test this string to params translation as its covered very well by the rails framework tests. Plus all testing methods that need params accept a hash in this manner without complaint making things easy for you.在本例中 params[:post] 是 {},
params[:user] 是 {:email =>; 'abc@abcd'},
params[:用户][:电子邮件] 是“abc@abcd”。
在这种情况下,params[:post][:user][:email] 是 'abc@abcd'
In this case params[:post] is {},
params[:user] is {:email => 'abc@abcd'},
params[:user][:email] is 'abc@abcd'.
In this case params[:post][:user][:email] is 'abc@abcd'