gpt4 book ai didi

ruby-on-rails - 如何在 Rails 应用程序中使用 Capybara 测试多选选择框?

转载 作者:数据小太阳 更新时间:2023-10-29 08:30:57 25 4
gpt4 key购买 nike

我和一位同事一直在努力弄清楚如何使用 select 元素测试 Rails 表单,我们有时希望它成为标准的选择下拉列表,有时又希望成为多选框。

我们可以在 RSpec 功能规范中添加什么来测试它?这是我们要测试的代码:

    <% if @use_multi_select %>
<%= select_tag :program, options_for_select(@programs), { multiple: true } %>
<% else %>
<%= select_tag :program, options_for_select(@programs) %>
<% end %>

下面是我对测试的想法:

    context 'when @use_multi-select == true' do
it 'displays a multi-select select box' do
expect(page).to have_select('program').with_options(multiple: true)
end
end

context 'when @use_multi-select == false' do
it 'displays a standard select box' do
expect(page).to have_select('program').with_options(multiple: false)
end
end

但是整个 with_options 的东西不是那样工作的。有没有人以前这样做过并找到了可行的解决方案或解决方法?

最佳答案

with_options 是一个选项,可以传递给 have_select(不是在 have_select 上调用的方法)以检查特定选项元素是否是选择的子元素(其他元素也可能存在)

要测试一个选择是否是多个你可以做

expect(page.find(:select, 'program')[:multiple]).to be_truthy

显然是相反的 be_falsey

如果你运行的是 Capybara 2.6.x,你可以这样做

Capybara.modify_selector(:select) do
filter(:multiple, boolean: true) { |node, value| !(value ^ node[:multiple]) }
end

这会让你做

expect(page).to have_select('program', multiple: true)

:multiple 过滤器可能会在发布时包含在 Capybara 2.7 中

关于ruby-on-rails - 如何在 Rails 应用程序中使用 Capybara 测试多选选择框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35180660/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com