你如何反驳 Capybara 的 has_select 选项的存在?
How do you refute the presence of an option with Capybara's has_select?
假设您有一个仅包含以下选项的 select 列表:
<option>Second Fake Option</option>
并且您想断言 select 列表 不 包含文本为 "Fake Option":
的选项
<option>Fake Option</option>
当有人这样反驳时:
refute has_select?('list_id',
with_options: ['Fake Option'])
测试失败。似乎 Capybara 成功地 部分 将 text Fake Option
与文本 Second Fake Option
相匹配。更进一步,以下也失败了:
refute has_select?('select_id',
with_options: [''])
但以下通过:
refute has_select?('select_id',
with_options: ['BORK'])
with_options:
和 options:
的文档描述了我们尝试匹配的 列表 选项的行为,但没有说明这一点部分匹配文本本身的行为。 Other questions here on SO 引用相同的记录行为...但不解决选项文本的反驳或匹配问题。
虽然我可以 assert
使用 options:
相反的行为,例如:
assert has_select?('select_id',
options: ['Second Fake Option'])
当您有一个很长的 select 列表并且想要反驳列表中某个特定选项的存在时,这可能会很痛苦。
如何正确地反驳 select 列表中特定选项的存在?
部分文本匹配是默认行为,但可以使用 :exact 选项覆盖。此外,与其反驳谓词,不如调用 refute_select
——错误消息会好得多
refute_select('select_id', with_options: ['Fake Option'], exact: true)
注意:如果页面上没有 'select_id' select 元素,这也会通过,这可能不是您想要的。如果你想验证 select 确实存在但它没有特定的选项那么像
select = find_field('select_id')
refute_selector(select, :option, 'Fake Option', exact: true)
可能是你想要的,这也可以通过匹配来完成
select = find_select('select_id')
refute_matches_selector(select, :select, with_options['Fake Option'], exact: true)
假设您有一个仅包含以下选项的 select 列表:
<option>Second Fake Option</option>
并且您想断言 select 列表 不 包含文本为 "Fake Option":
的选项<option>Fake Option</option>
当有人这样反驳时:
refute has_select?('list_id',
with_options: ['Fake Option'])
测试失败。似乎 Capybara 成功地 部分 将 text Fake Option
与文本 Second Fake Option
相匹配。更进一步,以下也失败了:
refute has_select?('select_id',
with_options: [''])
但以下通过:
refute has_select?('select_id',
with_options: ['BORK'])
with_options:
和 options:
的文档描述了我们尝试匹配的 列表 选项的行为,但没有说明这一点部分匹配文本本身的行为。 Other questions here on SO 引用相同的记录行为...但不解决选项文本的反驳或匹配问题。
虽然我可以 assert
使用 options:
相反的行为,例如:
assert has_select?('select_id',
options: ['Second Fake Option'])
当您有一个很长的 select 列表并且想要反驳列表中某个特定选项的存在时,这可能会很痛苦。
如何正确地反驳 select 列表中特定选项的存在?
部分文本匹配是默认行为,但可以使用 :exact 选项覆盖。此外,与其反驳谓词,不如调用 refute_select
——错误消息会好得多
refute_select('select_id', with_options: ['Fake Option'], exact: true)
注意:如果页面上没有 'select_id' select 元素,这也会通过,这可能不是您想要的。如果你想验证 select 确实存在但它没有特定的选项那么像
select = find_field('select_id')
refute_selector(select, :option, 'Fake Option', exact: true)
可能是你想要的,这也可以通过匹配来完成
select = find_select('select_id')
refute_matches_selector(select, :select, with_options['Fake Option'], exact: true)