2016-07-31 65 views
0

我有一个生成的表单为什么在匹配/ id = “MyId ”/通过时,has_selector“#MyId”失败?

#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

尝试使用has_selector:css,“#object_field” –

+0

你看过你的帮助程序返回的实际字符串吗? –

+0

谢谢@FrederickCheung,是的,该字符串包含所有元素和窗体呈现预期。我无法让匹配器按照预期行事。 –

回答

0

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

it { expect(helper.my_helper).to have_selector "##{object_field}" } 
相关问题