问题描述
我正在尝试选择一个带有水豚的单选按钮,但找不到单选按钮。这是我的rspec测试,查看和错误。请注意,我正在使用工厂来提供用户,技能等。
I'm trying to select a radio button with capybara and it can't find the radio button. Here is my rspec test, view and error. Please note that I am using factories for user, skills, etc.
Rspec测试
scenario "user chooses a couple skills and moves on to bio" do
user = create(:user)
skill = create(:skill)
skill_two = create(:skill)
skill_three = create(:skill)
sign_in(user)
visit onboard_skills_path
choose(skill.name)
end
查看
<%= form_for(:onboard_skill, url: onboard_skills_path) do |f| %>
<ul>
<% @skills.each do |skill| %>
<li>
<%= check_box_tag("skill_ids[]", skill.id, current_user.onboard_skill_ids.include?(skill.id)) %>
<%= f.label(skill.name) %>
</li>
<% end %>
</ul>
<%= f.submit "Next >", class: "submit_skills" %>
<% end %>
我遇到的错误是:
Unable to find radio button "Development 1"
推荐答案
您正在使用选择
测试单选按钮,但是在您提供的查看代码中,您有 check_box_tag
。尝试将 check_box_tag
更改为 radio_button_tag
,或者如果您确实想要复选框,请使用 check
而不是选择
。
You are using choose
to test for a radio button but in your provided view code you have a check_box_tag
. Try either changing check_box_tag
to radio_button_tag
or if you really want a check box, use check
instead of choose
.
请注意,您还可以选择单选按钮或复选框通过使用find搜索ID。如果水豚没有通过标签名称找到它,这将很有帮助。试试:
Note that you can also select a radio button or check box by searching for the id using find. This helps when capybara does not find it by the label name. Try:
find(:css, "#skill_ids_[value='#{skill.id}']").set(true)
这篇关于如何在Rails应用程序中单击红宝石中的水豚的单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!