ArgumentError:断言消息必须是使用assert_select的String或Proc

发布于 2024-12-07 14:52:56 字数 745 浏览 0 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

画离情绘悲伤 2024-12-14 14:52:56

因此, assert_select 文档 显示了以下示例,传递了一个块:

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 元素的数量为零,那么基本上会失败。显然,您会更改条件以匹配您要测试的内容,但这会让您更接近您需要的吗?

So, the assert_select documentation shows the following example, passing a block in:

assert_select "ol" do |elements|
  elements.each do |element|
    assert_select element, "li", 4
  end
end

So what if you did something like...

assert_select 'h1' do |elements|
  elements.length == 0 ? fail
  elements.each do |element|
    element.text ~= /Key Dates/ ? fail
  end
end

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?

同尘 2024-12-14 14:52:56

如果您无法升级到无错误版本,您可以只传递第三个参数(消息),这样您就不会强制构建消息:

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."

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文