Francois Beausoleil
2007-Jan-24 19:19 UTC
Differences between assert_tag and assert_select
Hi all, I can't seem to make assert_select work for the more complex cases for me. Here's a sample: # View <%= link_to_remote 'Add new', :url => new_phone_url, :submit => 'phones_head' %> # Generated code: <a href="#" onclick="new Ajax.Request('http://test.host/admin/parties/phone/new', {asynchronous:true, evalScripts:true, parameters:Form.serialize('phones_head')}); return false;">Add new</a> # Succeeds: assert_tag :a, :attributes => {:onclick => Regexp.new(Regexp.escape(new_phone_url))}, :content => /add new/i # Fails assert_select "a[onclick=?]", Regexp.new(Regexp.escape(new_phone_url)), /add new/i test_link_to_add_address_exists(NewPartyViewTest) [/home/francois/src/config/../vendor/rails/actionpack/lib/action_controller/assertions/selector_assertions.rb:281:in `assert_select' test/functional/admin/parties_controller_test.rb:1518:in `test_link_to_add_address_exists']: Expected at least 1 elements, found 0. <false> is not true. There's probably something I'm not doing right, for sure. #assert_select is supposed to be so much easier to use, but I'm not find that at all. I just learned about "a[onclick*=?]", and I can make my test pass with that. But regexps sure are something I use very often. Thanks ! -- François Beausoleil http://blog.teksol.info/ http://piston.rubyforge.org/ --~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com 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 -~----------~----~----~----~------~----~------~--~---
On 1/24/07, Francois Beausoleil <francois.beausoleil-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi all, > > I can''t seem to make assert_select work for the more complex cases for me. > > Here''s a sample: > > # View > <%= link_to_remote ''Add new'', :url => new_phone_url, :submit => ''phones_head'' %> > > # Generated code: > <a href="#" onclick="new > Ajax.Request(''http://test.host/admin/parties/phone/new'', > {asynchronous:true, evalScripts:true, > parameters:Form.serialize(''phones_head'')}); return false;">Add new</a> > > # Succeeds: > assert_tag :a, :attributes => {:onclick => > Regexp.new(Regexp.escape(new_phone_url))}, :content => /add new/i > > # Fails > assert_select "a[onclick=?]", > Regexp.new(Regexp.escape(new_phone_url)), /add new/i > > test_link_to_add_address_exists(NewPartyViewTest) > [/home/francois/src/config/../vendor/rails/actionpack/lib/action_controller/assertions/selector_assertions.rb:281:in > `assert_select'' > test/functional/admin/parties_controller_test.rb:1518:in > `test_link_to_add_address_exists'']: > Expected at least 1 elements, found 0. > <false> is not true. > > There''s probably something I''m not doing right, for sure. > #assert_select is supposed to be so much easier to use, but I''m not > find that at all. I just learned about "a[onclick*=?]", and I can > make my test pass with that. But regexps sure are something I use > very often. > > Thanks ! > -- > François Beausoleil > http://blog.teksol.info/ > http://piston.rubyforge.org/ > > > >François, You probably don''t need to be this exact to test the accuracy of your view rendering. To make things easier you might want to just add a class to your anchor. assert_select ''a[class=my-link-class]'', /Add new/i Hope this helps. -- Zack Chandler http://depixelate.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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---
Francois Beausoleil
2007-Jan-24 20:54 UTC
Re: Differences between assert_tag and assert_select
Hello Zack, 2007/1/24, Zack Chandler <zackchandler@gmail.com>:> You probably don't need to be this exact to test the accuracy of your > view rendering. To make things easier you might want to just add a > class to your anchor. > > assert_select 'a[class=my-link-class]', /Add new/iThanks for replying. But unfortunately, I do need to be exact. Is there or isn't there a link on that page that allows adding a phone ? Putting a class isn't exactly the same. Thanks again ! -- François Beausoleil http://blog.teksol.info/ http://piston.rubyforge.org/ --~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com 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 -~----------~----~----~----~------~----~------~--~---
> # View > <%= link_to_remote ''Add new'', :url => new_phone_url, :submit => > ''phones_head'' %> > > # Generated code: > <a href="#" onclick="new > Ajax.Request(''http://test.host/admin/parties/phone/new'', > {asynchronous:true, evalScripts:true, > parameters:Form.serialize(''phones_head'')}); return false;">Add new</a> > > # Succeeds: > assert_tag :a, :attributes => {:onclick => > Regexp.new(Regexp.escape(new_phone_url))}, :content => /add new/i > > # Fails > assert_select "a[onclick=?]", > Regexp.new(Regexp.escape(new_phone_url)), /add new/iI don''t think you can pass a regular expression to your attribute selector, i.e. in a[onclick=XXX], XXX must be a string. Therefore I think you have to express the test for the attribute value and the test for the link''s text in two separate selector statements. Using a substitution value as you have done allows you to test the attribute''s value but precludes testing the link''s text in the same line (I think). This might do it: assert_select "a[onclick]", /add new/i assert_select "a[onclick=?]", Regexp.new(Regexp.escape(new_phone_url)) Hope that helps somehow. Regards, Andy Stewart --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Francois Beausoleil
2007-Jan-25 15:09 UTC
Re: Differences between assert_tag and assert_select
Hello Andy, 2007/1/25, Andrew Stewart <boss@airbladesoftware.com>:> I don't think you can pass a regular expression to your attribute > selector, i.e. in a[onclick=XXX], XXX must be a string. Therefore I > think you have to express the test for the attribute value and the > test for the link's text in two separate selector statements.http://labnotes.org/svn/public/ruby/rails_plugins/assert_select/cheat/assert_select.html Look at the "Substitution Values" section.> assert_select "a[onclick]", /add new/i > assert_select "a[onclick=?]", Regexp.new(Regexp.escape(new_phone_url))There's nothing that confirms that the same node was used here. It might be an entirely different one. The form on which this is checked has the capability of adding phones, addresses, tesimonials, invoices, payments and other objects too. And "Add new" is the standard text to add a new something. Thanks for the reply ! -- François Beausoleil http://blog.teksol.info/ http://piston.rubyforge.org/ --~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com 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 -~----------~----~----~----~------~----~------~--~---
How would I turn the following into using assert_select? assert_tag :tag => /input|textarea|select/, :attributes => { :name => /#{obj}\[#{field_name}\]|#{obj}\[#{field_name}\(\di\)\]/ } May it''s obvious and assert_select will take a regex like this but I couldn''t find any info. Thanks. Jason On 1/25/07, Francois Beausoleil <francois.beausoleil-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Hello Andy, > > 2007/1/25, Andrew Stewart <boss-rAnrN1QAkGGttCpgsWEBFlaTQe2KTcn/@public.gmane.org>: > > I don''t think you can pass a regular expression to your attribute > > selector, i.e. in a[onclick=XXX], XXX must be a string. Therefore I > > think you have to express the test for the attribute value and the > > test for the link''s text in two separate selector statements. > > > http://labnotes.org/svn/public/ruby/rails_plugins/assert_select/cheat/assert_select.html > > Look at the "Substitution Values" section. > > > assert_select "a[onclick]", /add new/i > > assert_select "a[onclick=?]", Regexp.new(Regexp.escape(new_phone_url)) > > There''s nothing that confirms that the same node was used here. It > might be an entirely different one. The form on which this is checked > has the capability of adding phones, addresses, tesimonials, invoices, > payments and other objects too. And "Add new" is the standard text to > add a new something. > > Thanks for the reply ! > -- > François Beausoleil > http://blog.teksol.info/ > http://piston.rubyforge.org/ > > > >--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---
Hello Francis,>> assert_select "a[onclick]", /add new/i >> assert_select "a[onclick=?]", Regexp.new(Regexp.escape >> (new_phone_url))Try this (it works for me): assert_select "a[onclick=?]", Regexp.new(".+#{Regexp.escape(new_phone_url)}.+")), :text => /add new/i The substitution value consumes the next argument, the regular expression, and one can test the text as usual. The trick seems to be -- at least on my system -- that the regexp substitution value does not behave as one would expect. It appears to assume anchors to the start and end of the attribute value even if one doesn''t put in those anchors. For example, your onclick attribute value is a long AJAXy string and you just want to match the new_phone_url somewhere inside it. Let''s say that URL is: /phones/123/new'' I originally assumed that this would be the correct regexp to use: /\/ phones\/\d+\/new/ But in my experiments I found assert_select behaves as if I had used: /^\/phones\/\d+\/new$/ So I added .+ to the start and end of my regexp to allow for the AJAXy stuff between the start of the onlick attribute''s value and the end, giving: /.+\/phones\/\d+\/new.+/ Does that make sense? And if it does, does it work for you? :) Regards, Andy Stewart --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Francois Beausoleil
2007-Jan-26 01:58 UTC
Re: Differences between assert_tag and assert_select
Hello Andrew, 2007/1/25, Andrew Stewart <boss@airbladesoftware.com>:> So I added .+ to the start and end of my regexp to allow for the > AJAXy stuff between the start of the onlick attribute's value and the > end, giving: > > /.+\/phones\/\d+\/new.+/ > > Does that make sense? And if it does, does it work for you? :)It makes perfect sense, and I changed only one thing from your solution. I used .* instead of .+, to allow for the fact that the string might be anywhere. Looking at Rails code, I can see this: # actionpack/lib/action_controller/vendor/html-scanner/html/selector.rb # Lines 682 and later (Rails 1.2.1) def attribute_match(equality, value) regexp = value.is_a?(Regexp) ? value : Regexp.escape(value.to_s) case equality when "=" then # Match the attribute value in full Regexp.new("^#{regexp}$") # ^^^ see this ? # ... end end So, using "a[onclick*=?]", Regexp is the right solution, then. Thanks for your digging around. Bye ! -- François Beausoleil http://blog.teksol.info/ http://piston.rubyforge.org/ --~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com 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 -~----------~----~----~----~------~----~------~--~---