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. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Colin Law
2010-Dec-07 20:43 UTC
Re: Use assert_select to test for the occurence of a string
On 7 December 2010 18:19, Jim Burgess <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> 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.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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Jim Burgess
2010-Dec-07 21:13 UTC
Re: Use assert_select to test for the occurence of a string
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 -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Marnen Laibow-Koser
2010-Dec-07 21:14 UTC
Re: Use assert_select to test for the occurence of a string
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. > JimView tests are a pain. I highly recommend Cucumber instead. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.