Ruby on rails 使用水豚和简单形式的won#x27进行测试;不要选中复选框
在我的水豚/rspec测试中,我很难从简单表单中检查服务条款框。Ruby on rails 使用水豚和简单形式的won#x27进行测试;不要选中复选框,ruby-on-rails,rspec,capybara,simple-form,Ruby On Rails,Rspec,Capybara,Simple Form,在我的水豚/rspec测试中,我很难从简单表单中检查服务条款框。 以下是我的验证: validates :terms_of_service, acceptance: true, allow_nil: false 如果我删除allow_nil:false,那么即使未选中该框,规范也会全部通过。如果我离开它,验证会导致规范失败 以下是创建表单/复选框的代码: = f.label :terms_of_service, "I agree to the #{link_to 'Terms of Ser
以下是我的验证:
validates :terms_of_service, acceptance: true, allow_nil: false
如果我删除allow_nil:false,那么即使未选中该框,规范也会全部通过。如果我离开它,验证会导致规范失败
以下是创建表单/复选框的代码:
= f.label :terms_of_service, "I agree to the #{link_to 'Terms of Service', terms_of_service_path, :target => "_blank"}".html_safe
= f.check_box :terms_of_service
生成的html:
<label for="candidate_terms_of_service">I agree to the <a href="/terms_of_service" target="_blank">Terms of Service</a></label>
<input name="candidate[terms_of_service]" type="hidden" value="0">
<input id="candidate_terms_of_service" name="candidate[terms_of_service]" type="checkbox" value="1">
以及由此产生的故障:
Failure/Error: let(:candidate) { create(:candidate) }
ActiveRecord::RecordInvalid:
Validation failed: Terms of service must be accepted
如何选中此框?请尝试:
find('#candidate_terms_of_service').check
这个特殊的简单表单字段给了我很多痛苦,虽然我发现了几种不同的查询和设置方法,但它们都不起作用(有些提到,有些来自) 我发现以下内容实际上适用于带有RSpec的简单布尔形式:
find(:xpath, "//label[@for='candidate_terms_of_service']").click
考虑到不同的Web驱动程序和给出的总体解决方案,为了完整性起见,以下是找到的其他解决方案,但会导致无效选择器错误。输入选择器实际上会因为无法检查simple_表单的默认隐藏输入而出错;此错误是有意义的,因为标签是可见的最上面的元素
find('#active_form_terms_of_service').set(true)
# or select by label
find('label[for=active_form[terms_of_service]').click
# and select by label and value if multiple boxes
# with the same ID (not sure why this would happen)
find("label[value='1']").click
# or select the input with xpath
find(:xpath, "//input[value='1']").set(true)
不走运。我加入了跑步名单。值得一提的是,我尝试删除了allow_nil:false,这并不奇怪,它让规格通过,而我没有选中该框。你尝试过单击而不是选中吗?我尝试过。不走运。最后,我只是通过点击“注册”来更改服务条款语言,以避免所有的麻烦。这有点好笑。:)当我意识到我可以用另一种方式做这件事时,脸上露出了笑容。
find('#active_form_terms_of_service').set(true)
# or select by label
find('label[for=active_form[terms_of_service]').click
# and select by label and value if multiple boxes
# with the same ID (not sure why this would happen)
find("label[value='1']").click
# or select the input with xpath
find(:xpath, "//input[value='1']").set(true)