Use assert_select to test for the occurence of a string

Hi,

How would I use assert select to test for the occurrence of the string 'DUS' in a 'p' tag about half way down my view?

I tried:

def test_should_display_airport_names_in_show   get :show, :id => flights(:dus_muc).id   assert_select 'p', 'DUS' end

but Rails is just finding the first 'p' tag (which isn't the one I'm looking for) and returning false.

1) Failure: test_should_display_airport_names_in_show(FlightsControllerTest) [/test/functional/flights_controller_test.rb:75]: <"DUS"> expected but was <"Nr:\n RA447">. <false> is not true.

Thanks in advance.

The error is a little misleading. Your assert_select will return true if _any_ p tag contains (exactly) DUS. When it fails it just shows the contents of the first which is not very helpful. Note that the tag must contain exactly DUS. Any extra spaces or newlines or whatever will cause it to fail. You can provide a regular expression instead of a string to allow more complex checks.

Colin

Oh, right. Thanks a lot. That had been driving me mad (it's an exercise in a book I'm reading). I changed the test method to:

def test_should_show_airport_names_in_show   get :show, :id => flights(:dus_muc).id   assert_select 'p', "Departure airport:\n DUS" end

and it works perfectly. Thanks again. Jim

Jim Burgess wrote in post #967016:

Oh, right. Thanks a lot. That had been driving me mad (it's an exercise in a book I'm reading). I changed the test method to:

def test_should_display_airport_names_in_show   get :show, :id => flights(:dus_muc).id   assert_select 'p', "Departure airport:\n DUS" end

and it works perfectly. Thanks again. Jim

View tests are a pain. I highly recommend Cucumber instead.

Best,