Rails: assert_select 在测试具有数值的属性时给出 nokogiri 错误
Rails: assert_select gives nokogiri error when testing for attribute with numeric value
在我的 rails 集成测试中,我试图测试是否存在具有给定值(即对象 ID,整数)的复选框。
此行:assert_select "input[type=checkbox][value=#{c.id.to_s}]"
生成以下错误:
Nokogiri::CSS::SyntaxError: unexpected '283557850' after 'equal'
(283557850 is the ID of the object).
令人困惑的是,我只在测试数值时遇到此错误。如果我输入一些字母,例如 assert_select "input[type=checkbox][value=test#{c.id.to_s}]"
,我将不再收到该错误(显然测试失败,因为我的复选框的值实际上不是“test283557850”)。
有什么想法吗?
附件是我的assert_xpath
。它比 assert_select
更好,因为 XPath 将 XML 视为可以进行任意复杂查询的数据库。
您的查询将是 assert_xpath "//input[@type='checkbox' and @value='#{c.id.to_s}']"
,或者更好的是 assert_xpath '//input[@type="checkbox" and @value=$value]', value: c.id.to_s
。
assert_xpath
也可以嵌套。例如:
assert_xpath '//label[ contains(text(), "Group:") ]' do
assert_xpath 'select[ "group_id" = @name ]' do
assert_xpath 'option[ "Anonymous users" = text() and "13" = @value ]'
assert_xpath 'option[ "B Team" = text() and "11" = @value and "selected" = @selected ]'
end
end
将此粘贴到您的 test_helper.rb
文件中:
class ActiveSupport::TestCase
def assert_xml(xml)
@xdoc = Nokogiri::XML(xml, nil, nil, Nokogiri::XML::ParseOptions::STRICT)
refute_nil @xdoc
return @xdoc
end
def assert_html(html=nil)
html ||= @response.body
@xdoc = Nokogiri::HTML(html, nil, nil, Nokogiri::XML::ParseOptions::STRICT)
refute_nil @xdoc
return @xdoc
end
def assert_xpath(path, replacements={}, &block)
@xdoc ||= nil # Avoid a dumb warning
@xdoc or assert_html # Because assert_html snags @response.body for us
element = @xdoc.at_xpath(path, nil, replacements)
unless element
complaint = "Element expected in:\n`#{@xdoc}`\nat:\n`#{path}`"
replacements.any? and complaint += "\nwith: " + replacements.pretty_inspect
raise Minitest::Assertion, complaint
end
if block
begin
waz_xdoc = @xdoc
@xdoc = element
block.call(element)
ensure
@xdoc = waz_xdoc
end
end
return element
end
def refute_xpath(path, replacements={}, &block)
@xdoc ||= nil # Avoid a dumb warning
@xdoc or assert_html # Because assert_html snags @response.body for us
element = @xdoc.at_xpath(path, nil, replacements)
if element
complaint = "Element not expected in:\n`#{@xdoc}`\nat:\n`#{path}`"
replacements.any? and complaint += "\nwith: " + replacements.pretty_inspect
raise Minitest::Assertion, complaint
end
end
end
问题是 input[type=checkbox][value=123]
不是有效的 CSS 选择器。查询属性时,值必须以字母开头或用引号引起来。
assert_select "input[type=checkbox][value='#{c.id.to_s}']"
您可以使用浏览器控制台和 document.querySelectorAll()
.
解决此类问题
在我的 rails 集成测试中,我试图测试是否存在具有给定值(即对象 ID,整数)的复选框。
此行:assert_select "input[type=checkbox][value=#{c.id.to_s}]"
生成以下错误:
Nokogiri::CSS::SyntaxError: unexpected '283557850' after 'equal'
(283557850 is the ID of the object).
令人困惑的是,我只在测试数值时遇到此错误。如果我输入一些字母,例如 assert_select "input[type=checkbox][value=test#{c.id.to_s}]"
,我将不再收到该错误(显然测试失败,因为我的复选框的值实际上不是“test283557850”)。
有什么想法吗?
附件是我的assert_xpath
。它比 assert_select
更好,因为 XPath 将 XML 视为可以进行任意复杂查询的数据库。
您的查询将是 assert_xpath "//input[@type='checkbox' and @value='#{c.id.to_s}']"
,或者更好的是 assert_xpath '//input[@type="checkbox" and @value=$value]', value: c.id.to_s
。
assert_xpath
也可以嵌套。例如:
assert_xpath '//label[ contains(text(), "Group:") ]' do
assert_xpath 'select[ "group_id" = @name ]' do
assert_xpath 'option[ "Anonymous users" = text() and "13" = @value ]'
assert_xpath 'option[ "B Team" = text() and "11" = @value and "selected" = @selected ]'
end
end
将此粘贴到您的 test_helper.rb
文件中:
class ActiveSupport::TestCase
def assert_xml(xml)
@xdoc = Nokogiri::XML(xml, nil, nil, Nokogiri::XML::ParseOptions::STRICT)
refute_nil @xdoc
return @xdoc
end
def assert_html(html=nil)
html ||= @response.body
@xdoc = Nokogiri::HTML(html, nil, nil, Nokogiri::XML::ParseOptions::STRICT)
refute_nil @xdoc
return @xdoc
end
def assert_xpath(path, replacements={}, &block)
@xdoc ||= nil # Avoid a dumb warning
@xdoc or assert_html # Because assert_html snags @response.body for us
element = @xdoc.at_xpath(path, nil, replacements)
unless element
complaint = "Element expected in:\n`#{@xdoc}`\nat:\n`#{path}`"
replacements.any? and complaint += "\nwith: " + replacements.pretty_inspect
raise Minitest::Assertion, complaint
end
if block
begin
waz_xdoc = @xdoc
@xdoc = element
block.call(element)
ensure
@xdoc = waz_xdoc
end
end
return element
end
def refute_xpath(path, replacements={}, &block)
@xdoc ||= nil # Avoid a dumb warning
@xdoc or assert_html # Because assert_html snags @response.body for us
element = @xdoc.at_xpath(path, nil, replacements)
if element
complaint = "Element not expected in:\n`#{@xdoc}`\nat:\n`#{path}`"
replacements.any? and complaint += "\nwith: " + replacements.pretty_inspect
raise Minitest::Assertion, complaint
end
end
end
问题是 input[type=checkbox][value=123]
不是有效的 CSS 选择器。查询属性时,值必须以字母开头或用引号引起来。
assert_select "input[type=checkbox][value='#{c.id.to_s}']"
您可以使用浏览器控制台和 document.querySelectorAll()
.