Testing render :update

Hey guys.

I would like to test the following behavior:

   render :update do |page|
        page.replace_html 'errors', :partial => 'signup_errors', 

:locals
=> { :errors => ‘errors’}
end
}

I’m doing:

controller.should_receive(:render).with(:update)

But I am not sure on how to test the block. Maybe checking out what is
the
class of the page var and setting up an expection on it?

Any suggestions appreciated,

Marcelo.

On 2010-06-14 6:40 PM, Marcelo de Moraes S. wrote:

I’m doing:

controller.should_receive(:render).with(:update)

But I am not sure on how to test the block. Maybe checking out what is
the class of the page var and setting up an expection on it?

Any suggestions appreciated,

Marcelo.

I don’t know if this is the “right” way to do it, but if I’m going to
test this code:

         render :update do |page|
             page.replace :flash_messages, :partial =>

‘_partials/web/layouts/flash_messages’
end

I use this:

         it 'should render update' do
             page = mock('page')
             page.should_receive(:replace).with(:flash_messages,

:partial => ‘_partials/web/layouts/flash_messages’)

controller.should_receive(:render).with(:update).and_yield(page)
xhr :post, :create,
end

Peace,
Phillip

On Jun 14, 2010, at 6:40 PM, Marcelo de Moraes S. wrote:

controller.should_receive(:render).with(:update)

But I am not sure on how to test the block. Maybe checking out what is the class of the page var and setting up an expection on it?

Hmmm. The common idiom for this is:

page = double(‘page’)
controller.stub(:render).with(:update).and_yield(page)
page.should_receive(:replace_html).with(…)

This does not seem to work with rspec-2/rails-3. Don’t know why yet
(won’t be able to look for a bit), but my guess is that something (like
rspec :slight_smile: ) is adding render() to the controller even later than this
stub does. I’ll follow up if I learn something.

HTH,
David

Thanks for the heads out, David :slight_smile:

Por nada!

ps - em inglês, a frase é “heads up” :slight_smile:

On Jun 14, 2010, at 7:05 PM, David C. wrote:

page.should_receive(:replace_html).with(…)

This does not seem to work with rspec-2/rails-3. Don’t know why yet (won’t be able to look for a bit), but my guess is that something (like rspec :slight_smile: ) is adding render() to the controller even later than this stub does. I’ll follow up if I learn something.

Looks like action_pack calls render twice (the 2nd time in
action_controller/metal/implicit_render.rb:10:in `default_render’), so
we’ve got to stub the :render call twice:

page = double(‘page’)
controller.stub(:render)
controller.stub(:render).with(:update).and_yield(page)
page.should_receive(:replace_html).with(…)

Bummer. It seems like this is a rails bug (render should only get called
once, and there is code that prevents it from being called twice, but
not all of the time), but I’m not sure if I can make that case or not.
I’ll try :slight_smile:

Cheers,
David

David C. wrote:

Looks like action_pack calls render twice (the 2nd time in
action_controller/metal/implicit_render.rb:10:in `default_render’), so
we’ve got to stub the :render call twice:

page = double(‘page’)
controller.stub(:render)
controller.stub(:render).with(:update).and_yield(page)
page.should_receive(:replace_html).with(…)

Bummer. It seems like this is a rails bug (render should only get called
once, and there is code that prevents it from being called twice, but
not all of the time), but I’m not sure if I can make that case or not.
I’ll try :slight_smile:

Have this problem been reported to the rails project. I am not using
rspec, but trying to stub away rendering and run into the same
situation; have to call #expects twice using Mocha framework.

Could it be this ticket:
https://rails.lighthouseapp.com/projects/8994/tickets/2279-render-layout-with-block-and-multiple-yields

Ah!

Thanks for the heads up about heads out :wink:

Obrigado,

Marcelo.