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


rspec version: 2.4.0

Given this helper - snippet:

def participants_summary table
show_short_participant_link(participant) }.join(’, ')

def show_table_link(table)
link_to(table.name, :action => :show, :id => table)

and this spec

describe ‘participants_summary’ do
let(:table) { new_table :participants => [
new_participant(:id=> 1, :name =>
new_participant(:id=> 2, :name =>
‘name2’) ] }

  it "includes the names of all participants" do
    participants = participants_summary table

include(link_to(‘name1’, :controller=>‘person’,:action=>‘show’,:id=>1))
include(link_to(‘name2’, :controller=>‘person’,:action=>‘show’,:id=>2))

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(’, ')

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

module RSpec::Rails::HelperExampleGroup
include HelperRenderer

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

Rob A.

Software Developer
[email protected]
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 [email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs