ArgumentError:断言消息必须是使用assert_select的String或Proc
我正在使用 testunit 2.4.0 为 Rails 3.1 应用程序编写控制器测试。
我想断言某个标题不会出现在页面上。
我像这样使用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的实例
并将其传递给测试单元的 assert
。然而,在 testunit 的 2.2 版本(2011 年 2 月)中添加了检查,用于检查传入消息的类型。这些检查会触发上面看到的 ArgumentError
。
我不确定错误是否在于测试单元过于严格或assert_select传递了错误的对象类型。
您能建议如何最好地跟进此事吗?有什么解决方法吗?
I'm writing a controller test for a rails 3.1 app using testunit 2.4.0.
I want to assert that a certain heading does not appear on the page.
I'm using assert_select like this:
assert_select 'h1', {:text => /Key Dates/, :count => 0}
and getting the following error:
ArgumentError: assertion message must be String or Proc: <</Key Dates/>
expected but was <"Planner Maternity leave">.>(<Test::Unit::Assertions::AssertionMessage>)
I've tracked this down to the fact that assert_select
calls build_message
which creates an instance of AssertionMessage
and passes it through to test-unit's assert
. However in version 2.2 of testunit (Feb 2011) checks were added which check the type of the message passed in. These checks trigger the ArgumentError
seen above.
I'm not sure whether the mistake lies with test-unit being over-strict or assert_select passing the wrong object type.
Can you advise how best to follow this up? Any work-arounds?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因此, assert_select 文档 显示了以下示例,传递了一个块:
那又怎样如果你做了类似的事情...
如果它找到模式或者如果
h1
元素的数量为零,那么基本上会失败。显然,您会更改条件以匹配您要测试的内容,但这会让您更接近您需要的吗?So, the assert_select documentation shows the following example, passing a block in:
So what if you did something like...
Which basically fails if it finds the pattern OR if the number of
h1
elements is zero. Obviously you would change the conditions to match what it is you're trying to test for, but does that get you any closer to what you need?如果您无法升级到无错误版本,您可以只传递第三个参数(消息),这样您就不会强制构建消息:
assert_select 'h1', {:文字 => /关键日期/, :count => 0},“发现意外的关键日期。”
If you cannot upgrade to a bug-free version, you can just pass a third argument (the message), so you do not force the message be built:
assert_select 'h1', {:text => /Key Dates/, :count => 0}, "Unexpected Key Dates found."