我正在使用 testunit 2.4.0 为 Rails 3.1 应用编写 Controller 测试。
我想断言某个标题没有出现在页面上。
我像这样使用 assert_select:
assert_select 'h1', {:text => /Key Dates/, :count => 0}
出现以下错误:
ArgumentError: assertion message must be String or Proc: <</Key Dates/>
expected but was <"Planner Maternity leave">.>(<Test::Unit::Assertions::AssertionMessage>)
我已经追踪到 assert_select
调用 build_message
的事实,它创建了 AssertionMessage
的实例并将其传递给测试-单位的断言
。然而,在 testunit 的 2.2 版(2011 年 2 月)中添加了检查,用于检查传入的消息的类型。这些检查会触发上面看到的 ArgumentError
。
我不确定错误是在于测试单元过于严格还是 assert_select 传递了错误的对象类型。
您能建议如何最好地跟进吗?有什么变通办法吗?
最佳答案
因此,assert_select documentation显示以下示例,传入一个 block :
assert_select "ol" do |elements|
elements.each do |element|
assert_select element, "li", 4
end
end
那么如果你做了类似...的事情呢
assert_select 'h1' do |elements|
elements.length == 0 ? fail
elements.each do |element|
element.text ~= /Key Dates/ ? fail
end
end
如果找到模式或如果 h1
元素的数量为零,这基本上会失败。显然,您会更改条件以匹配您要测试的内容,但这是否让您更接近您的需要?
关于ruby-on-rails - 参数错误 : assertion message must be String or Proc using assert_select,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7597931/