我有一个生成表单的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。
答案 0 :(得分:0)
我理解,object_field是包含元素id的变量。如果是,那么请尝试下一步:
it { expect( helper.my_helper ).to have_selector "##{object_field}" }