【发布时间】:2021-04-30 13:19:11
【问题描述】:
我正在使用 Capybara,我正在使用以下命令从下拉列表中选择一个项目:
select "Some Option", from: "client_id", visible: :all
这是我用来生成选择的视图代码:
<%= form_with(url: '#', method: 'GET') do |f| %>
<%= f.select :client_id,
options_for_select(
Client.accessible_by(current_ability)
.map{|c| [c.name, c.id]}, selected_client_id
),
{include_blank: defined?(include_blank) ? include_blank : false},
{
class: "form-control selectpicker border",
id: "client_id",
onchange: "this.form.submit()",
data: {'live-search': true, style: "btn-white"}
}
%>
<% end %>
当我自己在页面上与之交互时,它可以正常呈现并按预期工作。
我认为问题源于此:onchange: "this.form.submit()"
当 Capybara 从 select 中选择一个选项时,它应该提交表单,然后页面应该使用查询字符串 /?client_id=X 重新加载,但这不是正在发生的事情,我不知道为什么。
当我查看错误提供的屏幕截图时,似乎 Capybara 甚至没有从下拉列表中选择任何内容,它只是说“未选择任何内容”。
这是水豚测试:
def check_grand_total(expected_value)
visit current_path
click_link "Balance"
# it seems as though Capybara is not waiting for this to finish... but I don't know, just my theory
# this is supposed to trigger: this.form.submit() on the dropdown
select "Some Client", from: "client_id", visible: :all
actual_value = page.first('.grand-total').text
assert_equal(expected_value, actual_value)
end
错误:
E
Error:
OrdersTest#test_deposit_orders:
Capybara::ExpectationNotMet: expected to find css ".grand-total" at least 1 time but there were no matches
test/system/helpers/balance_test_helper.rb:10:in `check_grand_total'
test/system/orders_test.rb:113:in `block in <class:OrdersTest>'
我在想也许 Capybara 出于某种原因实际上并未触发 change 事件。
【问题讨论】:
-
@engineersmnky 确定添加