RSpec Controller Testing - render_templte Issue

Hi!

I am writing controller specs and getting a weird expectation failure.
Here is the example. Please read the comments for details:

it ‘renders children/_show partial’ do
post :create, :child => @child

  # The line "response.should render_template('children/_show')"

gives
# me error:
# ChildrenController when the enrollment form data is valid
renders
# children/_show partial’ FAILED
# expected “children/_show”, got
“children/_show_print_layout.html.erb”
response.should render_template(‘children/_show’)

  # This is weird. _show partial contains a button 'Print Form' and

next #assertion passes, but the one above fails.
response.should have_selector( ‘input’,
:type => ‘submit’,
:value => ‘Print Form’ )
response.should be_success
end

Many Thanks!

On Mon, Jul 12, 2010 at 9:44 AM, Rails L. [email protected]
wrote:

   # me error:
   response.should have_selector( ‘input’,
                  :type => ‘submit’,
                  :value => ‘Print Form’ )
   response.should be_success
  end

Many Thanks!

Please include your controller code for create! :d


Make Haste Slowly

On Jul 12, 2010, at 11:11 AM, Rails L. [email protected]
wrote:

Many Thanks!
I figure out the issue.
My controller has render_to_string that renders a pdf and then it
renders the “_show” partial. Now there is another problem. How I can
test both renders?

The format param is used for this case - it is used to set content-
type properly - it is getting set by a route into a param var. Iirc
format.pdf is possible aswell - I suspect that the mime type I’d
already registered - it may not be.

Curtis j Schofield wrote:

On Mon, Jul 12, 2010 at 9:44 AM, Rails L. [email protected]
wrote:

   # me error:
   response.should have_selector( ‘input’,
                  :type => ‘submit’,
                  :value => ‘Print Form’ )
   response.should be_success
  end

Many Thanks!

Please include your controller code for create! :d


Make Haste Slowly

Hi Curtis,

I figure out the issue.
My controller has render_to_string that renders a pdf and then it
renders the “_show” partial. Now there is another problem. How I can
test both renders?

Here is the controller code:
def create
@page_title = ‘ASQ Enrollment Form’
@child = Child.new(params[:child])

#Convert string to date.
@child.EnrollmentDate = Date.strptime(@child.EnrollmentDate, 

‘%Y-%m-%d’)

#Concatenate the date components and assign the complete date to 

ChildDob field
@child.ChildDob = Date.strptime((params[:child][‘ChildDob(2i)’] +
‘-’ + params[:child][‘ChildDob(3i)’] + ‘-’ +
params[:child][‘ChildDob(1i)’]), ‘%m-%d-%Y’) unless
((params[:child][‘ChildDob(1i)’]).blank? ||
(params[:child][‘ChildDob(2i)’]).blank? ||
(params[:child][‘ChildDob(3i)’]).blank?)

respond_to do |format|
  if @child.valid?
    FormMailer.deliver_enrollment_form(Date.today.strftime("%m-%d-%Y").to_s 
  • ’ - ASQ Enrollment Form - ’ + @child.ChildFirstName + ’ ’ +
    @child.ChildLastName, @child, generate_pdf(@child, false))
    flash.now[:notice] = ‘ASQ Enrollment form was successfully
    submitted’
    #format.html { redirect_to(@child, :notice => ‘Child was
    successfully created.’) }
    format.html { render :partial => ‘show’, :locals => { :child =>
    @child }, :layout => ‘children’ }
    format.xml { render :xml => @child, :status => :created,
    :location => @child }
    else
    format.html { render :action => ‘new’ }
    format.xml { render :xml => @child.errors, :status =>
    :unprocessable_entity }
    end
    end
    end