[rspec-users] [RAILS] [rspec 2.4.0] helper spec works but rails escapes all output

Rob Aldred Rob.aldred at stardotstar.com
Thu Feb 3 03:58:37 EST 2011


Hi
Since rails 3 all strings are escaped, unlike rails 2 where you had to use the h method
use .html_safe in your helper method to stop the output escaping

the test is correct

This article will explain a little bit more
http://markconnell.co.uk/posts/2010/02/rails-3-html-escaping

--
Rob Aldred

Software Developer
rob at stardotstar.com
twitter: stardotstar

47 Newton Street, Manchester, M1 1FT
T: +44 (0) 161 236 9740
___________________________________________________

This email and any files or ideas transmitted within it are sent in 
confidence and are intended solely for the use of the individual or 
entity to whom they are addressed. If you have received this email 
in error please notify the system manager at info at stardotstar.com

On 2 Feb 2011, at 14:42, Rob Westgeest wrote:

> Hi,
> 
> rspec version: 2.4.0
> 
> Given this helper - snippet:
> ----
>  def participants_summary table
>    table.participants.collect{|participant|
> show_short_participant_link(participant) }.join(', ')
>  end
> 
>  def show_table_link(table)
>     link_to(table.name, :action => :show, :id => table)
>  end
> ---
> and this spec
> ---
>  describe 'participants_summary' do
>    let(:table) { new_table :participants => [
>                                  new_participant(:id=> 1, :name =>
> 'name1'),
>                                  new_participant(:id=> 2, :name =>
> 'name2') ] }
> 
>      it "includes the names of all participants" do
>        participants = participants_summary table
>        participants.should
> include(link_to('name1', :controller=>'person',:action=>'show',:id=>1))
>        participants.should
> include(link_to('name2', :controller=>'person',:action=>'show',:id=>2))
>      end
>  end
> ---
> 
> The spec passes, but the real output is escaped.
> 
> the helper should look like this
> ----
>  def participants_summary table
>    raw table.participants.collect{|participant|
> show_short_participant_link(participant) }.join(', ')
>  end
> ---
> 
> I think (but i am not sure) that rspec should mimic Rails' behaviour
> in escaping all html from such helpers unless you put 'raw' in front.
> 
> I can fix this in my case doing:
> 
> render :text => participants_summary(table)
> rendered.should include(link_to('name1', :controller=.......etc
> 
> This actually appears to use the rails3 rendering engine, or at least
> it fails if i remove the 'raw' call.
> Sadly, theconsequence of this is inserting something like this for not
> all but quite a few helpers.
> 
> Does anybody know of another way of dealing with this. Do you think
> that this render => :text call should be (an implicitly or explicit)
> part of the HelperExampleGroup?
> 
> I have put it in like this in one of my spec/support -ing files
> 
> module HelperRenderer
>  # renders the text as view so that rails view rendering magic
>  # (like implicit HTML escaping) can take place
>  def render_helper(text)
>    render :text => text
>    rendered
>  end
> end
> 
> module RSpec::Rails::HelperExampleGroup
>  include HelperRenderer
> end
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users



More information about the rspec-users mailing list