【发布时间】:2018-05-29 19:15:12
【问题描述】:
我正在尝试使用 rspec 为 Bootstrap 表单编写测试。我有以下代码:
<div id="published" class="col-sm-auto">
<div class="custom-control custom-radio custom-control-inline">
<%= form.radio_button :published, true, checked: page.published?, class: 'custom-control-input' %>
<%= form.label :published, 'Published', value: true, class: 'custom-control-label' %>
</div>
<div class="custom-control custom-radio custom-control-inline">
<%= form.radio_button :published, false, checked: !page.published?, class: 'custom-control-input' %>
<%= form.label :published, 'Hidden', value: false, class: 'custom-control-label' %>
</div>
</div>
生成以下 HTML:
<div id="published" class="col-sm-auto">
<div class="custom-control custom-radio custom-control-inline">
<input class="custom-control-input" type="radio" value="true" name="page[published]" id="page_published_true" />
<label class="custom-control-label" for="page_published_true">Published</label>
</div>
<div class="custom-control custom-radio custom-control-inline">
<input class="custom-control-input" type="radio" value="false" checked="checked" name="page[published]" id="page_published_false" />
<label class="custom-control-label" for="page_published_false">Hidden</label>
</div>
</div>
当我运行此测试时,它失败并显示消息
it 'should default to unpublished' do
expect(page).to have_checked_field('Hidden')
end
给消息expected to find visible field "page[published]" that is checked and not disabled but there were no matches. Also found "", "", which matched the selector but not all filters.
在检查器中查看 HTML,该字段可见且未禁用,并且有一个与文本匹配的标签。我不知道这两个空字符串是关于什么的。
请有人告诉我为什么have_checked_field 不匹配?我真的不热衷于编写使用has_css 或has_xpath 来查找具有特定ID 的输入标签的更脆弱的测试——重点是用户应该看到一个标有“隐藏”的字段,它应该被检查,不管幕后发生了什么!
【问题讨论】:
-
您确定该字段在视口中实际可见吗?您是否在未能确认时捕获屏幕截图和/或 HTML?