简体   繁体   English

Minitest + Capybara:如何检查选项列表是否不包含选项?

[英]Minitest + Capybara: how to check if options list does not contain an option?

In my application i have a select tag containing some options:在我的应用程序中,我有一个 select 标签,其中包含一些选项:

<select class="form-control" id="select-course-js">
     <option>course-1</option> 
     <option>course-2</option>
     <option>course-3</option>
</select>

I would like to implement a system test that check if there is no course-4 option.我想实施一个系统测试来检查是否没有 course-4 选项。 Is it possible to do it with capybara?水豚可以做到吗? if so - how?如果是这样 - 怎么样? stack: rails5 + Minitest + Capybara堆栈:rails5 + Minitest + Capybara

Update.更新。 Solution (given by Thomas Walpole)解决方案(由 Thomas Walpole 提供)

assert_no_select('select-course-js', with_options: ['course-4'])

You can verify there is no select 'select-course-js' on the page with course-4 as an option via您可以通过以下方式验证页面上没有 select 'select-course-js' 选项为course-4

assert_no_select('select-course-js', with_options: ['course-4'])

however that won't also verify that the select-course-js select is on the page, so you probably want to do both但是,这也不会验证select-course-js select 是否在页面上,因此您可能想要同时执行这两个

assert_select('select-course-js')
assert_no_select('select-course-js', with_options: ['course-4'])

Another option would be to use the optional filter block另一种选择是使用可选的过滤器块

assert_select('select-course-js') do |el|
  el.has_no_selector?(:option, 'course-4')
end

but the error won't be as clear if the test fails.但是如果测试失败,错误就不会那么清楚了。

One way is to add id's to each option and check whether it exists...一种方法是为每个option添加 id 并检查它是否存在......

<select class="form-control" id="select-course-js">
  <option id="crs-1">course-1</option> 
  <option id="crs-2">course-2</option>
  <option id="crs-3">course-3</option>
</select>

Then just check whether crs-4 exists.然后只需检查crs-4是否存在。 The method you use will depend on your particular logic but here are a couple examples...您使用的方法将取决于您的特定逻辑,但这里有几个例子......

assert_no_css('#select-course-js #crs-4')
page.has_css?('#select-course-js #crs-4')

You can find other methods to use here您可以在此处找到其他使用方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM