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

Rob Westgeest rob.westgeest at gmail.com
Wed Feb 2 09:42:50 EST 2011


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


More information about the rspec-users mailing list