5

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?

Triad sou.
  • 2,969
  • 3
  • 23
  • 27
heathd
  • 2,741
  • 2
  • 14
  • 5

2 Answers2

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

Arsen7
  • 12,522
  • 2
  • 43
  • 60
0

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?

jefflunt
  • 33,527
  • 7
  • 88
  • 126