如何使用 rspec 和 Rails 3 在控制器规范中传递复选框集合值
使用 Rails 3 和 rspec。我在这样的视图中有一个表单..
<%= form_for current_account, { :url => admins_account_path(current_account), :method => 'put' } do %>
<div class="action">
<%= submit_tag "Save" %>
</div>
<table>
<tbody>
<% @accountships.each do |accountship| %>
<tr>
<td><%= check_box_tag "accountship_ids[]", accountship.id, accountship.admin? %></td>
<td><%= accountship.user.name %>
</tr>
<% end %>
</tbody>
</table>
<% end %>
在控制器中,我在accounts#update_admin 中使用此方法处理PUT。这一切都按预期进行。
@account.assign_administrators params[:accountship_ids]
我的问题是如何构造 rspec 中的参数来测试该控制器操作。到目前为止我所尝试的方法不起作用。这是我最近的尝试,但不起作用。
before(:each) do
# code that generates the ids, I know this is working from other tests ..
.
.
.
@attr = {
:accountship_ids => [
@admin_accountship.id,
@not_admin_accountship.id,
@signed_in_user_accountship.id
]
}
end
it "should assign admin to users in the list" do
# what should I be passing in as @attr?
put :update_admins, :id => @account, :accountship_ids => @attr
Accountship.find(@admin_accountship.id).admin.should be_true
Accountship.find(@owner_accountship.id).admin.should be_true
Accountship.find(@not_admin_accountship.id).admin.should_not be_true
end
我能够编写的所有需要表单复选框集合中的值的测试都失败了,而且很明显,当 rspec 测试发布数据时,whatever_accountship.admin 属性没有被更新。
提前致谢!
编辑
我偶然发现了解决方案。数组不应包含在哈希中,并且数组文字中的值需要首先转换为字符串,如下所示。
@attr = [
@admin_accountship.id.to_s,
@not_admin_accountship.id.to_s,
@signed_in_user_accountship.id.to_s
]
当我的其他测试可以接受成熟的对象(不需要字符串)时,有人理解为什么它们需要字符串吗?
另外,既然我知道了答案,我该如何处理我的问题呢?
With rails 3 and rspec. I have a form in a view like this ..
<%= form_for current_account, { :url => admins_account_path(current_account), :method => 'put' } do %>
<div class="action">
<%= submit_tag "Save" %>
</div>
<table>
<tbody>
<% @accountships.each do |accountship| %>
<tr>
<td><%= check_box_tag "accountship_ids[]", accountship.id, accountship.admin? %></td>
<td><%= accountship.user.name %>
</tr>
<% end %>
</tbody>
</table>
<% end %>
And in the controller, I handle the PUT with this method in accounts#update_admin. This is all working as expected.
@account.assign_administrators params[:accountship_ids]
My question is how do I construct the parameters in rspec to test that controller action. What I have tried so far is not working. Here's my latest try that doesn't work.
before(:each) do
# code that generates the ids, I know this is working from other tests ..
.
.
.
@attr = {
:accountship_ids => [
@admin_accountship.id,
@not_admin_accountship.id,
@signed_in_user_accountship.id
]
}
end
it "should assign admin to users in the list" do
# what should I be passing in as @attr?
put :update_admins, :id => @account, :accountship_ids => @attr
Accountship.find(@admin_accountship.id).admin.should be_true
Accountship.find(@owner_accountship.id).admin.should be_true
Accountship.find(@not_admin_accountship.id).admin.should_not be_true
end
All the tests I've been able to write that require values from the form checkbox collection are failing, and it's apparent that the whatever_accountship.admin attribute is not being updated when the rspec test is posting the data.
Thanks in advance!
EDIT
I stumbled onto the solution. The array shouldn't have been wrapped in a hash, and the values in the array literal needed to be converted to strings first, as below.
@attr = [
@admin_accountship.id.to_s,
@not_admin_accountship.id.to_s,
@signed_in_user_accountship.id.to_s
]
Anyone understand why they need to be strings when other tests I have can accept a full-blown object (no strings required)?
Also, what do I do to my question now that I know the answer?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来您正在将 params 哈希分配给实例变量,然后使用第一个哈希作为值创建一个新哈希,并将整个混乱传递到
put
语句中,当您需要做的所有事情时是传递原始参数。或者换句话说:put :update_admins, :id => @account.id,@attr
编辑
抱歉,漫长的一天。参数需要进入操作后的单个哈希中,因此:
put :update_admins, {:id=>@account.id}.merge(@attr)
EDIT 2
如果您在数组中传递字符串,则哈希语法将起作用:
您想用自己的答案解决问题,我认为您可以创建一个答案然后接受它。
It looks like you're assigning the params hash to an instance variable and then making a new hash with the first hash as the value and passing the whole mess in the
put
statement, when all you need to do is pass the original params. Or in other words:put :update_admins, :id => @account.id, @attr
EDIT
Sorry, long day. The params need to go into a single hash following the action, so:
put :update_admins, {:id=>@account.id}.merge(@attr)
EDIT 2
The hash syntax will work if you pass strings in the array:
If you want to resolve the question with your own answer, I think you can just create an answer and then accept it.