为什么当match / id = \“MyId \”/通过时,has_selector“#MyId”会失败?

时间:2016-07-31 08:25:25

标签: ruby-on-rails rspec capybara

我有一个生成表单的rails helper

#helpers/my_helper.rb
def build_form 
  form_for object, url: object_path, method: :post do |f|
    html = f.text_input :field
    html += f.submit
    html.html_safe
  end
end

测试这个助手

#spec/helpers/my_helper_spec.rb
describe MyHelper do
  it { expect( helper.my_helper ).to have_selector "form[action='#{objects_path}'][method='post']" } # PASSES
  it { expect( helper.my_helper ).to have_selector "#object_field" } #FAILS - but this should pass
  it { expect( helper.my_helper ).to match /id=\"object_field\"/ } # PASSES
end

我还在学习rspec,所以这可能是一个显而易见的问题。

为什么在输入上使用have_selector时这些测试失败了。然而,have_selector正确传递了表单标记,match传递了输入ID。

1 个答案:

答案 0 :(得分:0)

我理解,object_field是包含元素id的变量。如果是,那么请尝试下一步:

 it { expect( helper.my_helper ).to have_selector "##{object_field}" }