如何测试 ruby​​ on Rails 和assert_select 中的禁用字段?

Fot*_*ton 3 ruby integration-testing ruby-on-rails css-selectors

我想测试 Rails(5) 视图助手中生成的禁用字段是否存在

form_for(@metric) do |f|
  f.text_field :type, disabled: true
end
Run Code Online (Sandbox Code Playgroud)

它创建 HTML

<input id="metric_type" type="text" name="metric[type]" value="Gamfora::Metric::Point" disabled="disabled">
Run Code Online (Sandbox Code Playgroud)

大概应该只是

<input id="metric_type" type="text" name="metric[type]" value="Gamfora::Metric::Point" disabled>
Run Code Online (Sandbox Code Playgroud)

但没关系,可以完成工作。


在 Firebug 中,我验证 CSS 选择器是input#metric_type:disabled.

但是当我在控制器(+视图)测试中使用它时

assert_select "input#metric_type:disabled"
Run Code Online (Sandbox Code Playgroud)

我收到错误

RuntimeError: xmlXPathCompOpEval: function disabled not found
Run Code Online (Sandbox Code Playgroud)

有什么办法,如何测试ID选择的输入被禁用吗?

Fot*_*ton 7

一种解决方案是

assert_select ".field input#metric_type" do |input|
    assert input.attr("disabled").present?
end 
Run Code Online (Sandbox Code Playgroud)