Ed Ruder
2009-Jul-04 21:40 UTC
[rspec-users] Routes helpers work differently from helper examples compared to page rendering
I have a helper that calls a Rails route _url method. In the context of a helper spec file, the route helper method ends up calling url_for in rails/actionpack/lib/action_controller/base.rb. In the context of rendering a page, the route helper method calls url_for in rails/ actionpack/lib/action_view/helpers/url_helper.rb. Though very similar, they can return different results, as I found out. Specifically, ActionController::Base#url_for doesn''t do html escaping-- ActionView::Helpers::UrlHelper#url_for does, and has an additional parameter, :escape => true|false, to controller whether or not the escaping is done. This took some frustrating time to track down. Since the code path executed by helper examples is not the same as production, tests can produce invalid results. Also, some behavior cannot be tested, since the code generating the results is not being executed. Is this a bug in RSpec, or is there a "correct" way to test helpers that I''m missing?
David Chelimsky
2009-Jul-04 22:53 UTC
[rspec-users] Routes helpers work differently from helper examples compared to page rendering
On Sat, Jul 4, 2009 at 4:40 PM, Ed Ruder<ed.ruder at gmail.com> wrote:> I have a helper that calls a Rails route _url method. In the context > of a helper spec file, the route helper method ends up calling url_for > in rails/actionpack/lib/action_controller/base.rb. In the context of > rendering a page, the route helper method calls url_for in rails/ > actionpack/lib/action_view/helpers/url_helper.rb. Though very similar, > they can return different results, as I found out. > > Specifically, ActionController::Base#url_for doesn''t do html escaping-- > ActionView::Helpers::UrlHelper#url_for does, and has an additional > parameter, :escape => true|false, to controller whether or not the > escaping is done. > > This took some frustrating time to track down. > > Since the code path executed by helper examples is not the same as > production, tests can produce invalid results. Also, some behavior > cannot be tested, since the code generating the results is not being > executed. > > Is this a bug in RSpec, or is there a "correct" way to test helpers > that I''m missing?Not sure if this is directly related, but you may be interested in this ticket: https://rspec.lighthouseapp.com/projects/5645/tickets/843
Ed Ruder
2009-Jul-08 19:03 UTC
[rspec-users] Routes helpers work differently from helper examples compared to page rendering
I don''t think that ticket applies to my problem--as I read it, https://rspec.lighthouseapp.com/projects/5645/tickets/843 is about enhancing RSpec with additional routing tests/specs. My issue is that in the context of a helper spec, when calling route helpers (_url/_path methods) from a helper, a different Rails code path is executed, giving different results in some cases. This means that sometimes your helper specs pass when the helper "in the wild" doesn''t do what''s spec''d--you cannot trust your specs! Thoughts? Ed On Jul 4, 3:53?pm, David Chelimsky <dchelim... at gmail.com> wrote:> On Sat, Jul 4, 2009 at 4:40 PM, Ed Ruder<ed.ru... at gmail.com> wrote: > > I have a helper that calls a Rails route _url method. In the context > > of a helper spec file, the route helper method ends up calling url_for > > in rails/actionpack/lib/action_controller/base.rb. In the context of > > rendering a page, the route helper method calls url_for in rails/ > > actionpack/lib/action_view/helpers/url_helper.rb. Though very similar, > > they can return different results, as I found out. > > > Specifically, ActionController::Base#url_for doesn''t do html escaping-- > > ActionView::Helpers::UrlHelper#url_for does, and has an additional > > parameter, :escape => true|false, to controller whether or not the > > escaping is done. > > > This took some frustrating time to track down. > > > Since the code path executed by helper examples is not the same as > > production, tests can produce invalid results. Also, some behavior > > cannot be tested, since the code generating the results is not being > > executed. > > > Is this a bug in RSpec, or is there a "correct" way to test helpers > > that I''m missing? > > Not sure if this is directly related, but you may be interested in > this ticket:https://rspec.lighthouseapp.com/projects/5645/tickets/843 > _______________________________________________ > rspec-users mailing list > rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users
David Chelimsky
2009-Jul-15 23:09 UTC
[rspec-users] Routes helpers work differently from helper examples compared to page rendering
On Wed, Jul 8, 2009 at 2:03 PM, Ed Ruder<ed.ruder at gmail.com> wrote:> I don''t think that ticket applies to my problem--as I read it, > https://rspec.lighthouseapp.com/projects/5645/tickets/843 is about > enhancing RSpec with additional routing tests/specs. > > My issue is that in the context of a helper spec, when calling route > helpers (_url/_path methods) from a helper, a different Rails code > path is executed, giving different results in some cases. ?This means > that sometimes your helper specs pass when the helper "in the wild" > doesn''t do what''s spec''d--you cannot trust your specs! > > Thoughts?Seems like this is an rspec bug. Please file a ticket at http://rspec.lighthouseapp.com. Thanks, David> > Ed > > On Jul 4, 3:53?pm, David Chelimsky <dchelim... at gmail.com> wrote: >> On Sat, Jul 4, 2009 at 4:40 PM, Ed Ruder<ed.ru... at gmail.com> wrote: >> > I have a helper that calls a Rails route _url method. In the context >> > of a helper spec file, the route helper method ends up calling url_for >> > in rails/actionpack/lib/action_controller/base.rb. In the context of >> > rendering a page, the route helper method calls url_for in rails/ >> > actionpack/lib/action_view/helpers/url_helper.rb. Though very similar, >> > they can return different results, as I found out. >> >> > Specifically, ActionController::Base#url_for doesn''t do html escaping-- >> > ActionView::Helpers::UrlHelper#url_for does, and has an additional >> > parameter, :escape => true|false, to controller whether or not the >> > escaping is done. >> >> > This took some frustrating time to track down. >> >> > Since the code path executed by helper examples is not the same as >> > production, tests can produce invalid results. Also, some behavior >> > cannot be tested, since the code generating the results is not being >> > executed. >> >> > Is this a bug in RSpec, or is there a "correct" way to test helpers >> > that I''m missing? >> >> Not sure if this is directly related, but you may be interested in >> this ticket:https://rspec.lighthouseapp.com/projects/5645/tickets/843 >> _______________________________________________ >> rspec-users mailing list >> rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >