Forum: RSpec How to expect no layout

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Nick H. (Guest)
on 2008-11-11 03:25
(Received via mailing list)
Hey guys. I've told one of my controllers to not render a layout for a
certain action:
   layout false, :only => :map_info_window

Now I'm trying to spec that, but this:
   it 'should not render a layout' do
     controller.expect_render :layout
     do_get
   end

fails with this:
   Spec::Mocks::MockExpectationError in 'PropertiesController handling
GET /properties/1/map_info_window should not render a layout'
Mock 'expect_render_mock_proxy' expected :render with (:layout) once,
but received it 0 times

I'm using RSpec v1.1.4 . I know it's old, but I haven't gotten around
to upgrading yet!
-Nick
Fernando P. (Guest)
on 2008-11-11 12:46
Nick H. wrote:
> Hey guys. I've told one of my controllers to not render a layout for a
> certain action:
>    layout false, :only => :map_info_window
>
> Now I'm trying to spec that, but this:
>    it 'should not render a layout' do
>      controller.expect_render :layout
>      do_get
>    end
>
> fails with this:
>    Spec::Mocks::MockExpectationError in 'PropertiesController handling
> GET /properties/1/map_info_window should not render a layout'
> Mock 'expect_render_mock_proxy' expected :render with (:layout) once,
> but received it 0 times
>
> I'm using RSpec v1.1.4 . I know it's old, but I haven't gotten around
> to upgrading yet!
> -Nick

Try:
controller.should_receive(:layout).with(false, {:only =>
:map_info_window})
Nick H. (Guest)
on 2008-11-11 15:52
(Received via mailing list)
On 2008-11-11, at 05:46, Fernando P. wrote:
>>   end
>
> Try:
> controller.should_receive(:layout).with(false, {:only =>
> :map_info_window})

Hi Fernando. Unfortunately, that didn't work either. It seems that the
controller object in the "controller" variable doesn't actually
receive the call to #layout :

Spec::Mocks::MockExpectationError in 'PropertiesController handling
GET /properties/1/map_info_window should not render a layout'
Mock 'PropertiesController' expected :layout with (false,
{:only=>:map_info_window}) once, but received it 0 times

Thanks,
Nick
David C. (Guest)
on 2008-11-11 16:22
(Received via mailing list)
On Tue, Nov 11, 2008 at 4:46 AM, Fernando P. 
<removed_email_address@domain.invalid>
wrote:
>>
> Try:
> controller.should_receive(:layout).with(false, {:only =>
> :map_info_window})

Declarations like #layout happen when the class definition is
interpreted (when the class is loaded) so you can't really intercept
them the same way you do with calls that happen as a result of an
action or an instance method call.

One trick for spec'ing class level declarations is to set the
expectation and reload the class file. I've seen this used
successfully with models:

ActiveRecord::Base.should_receive(:has_many).with(:cows)
load RAILS_ROOT + "/app/models/cowboy.rb"

Sadly, I've not seen that succeed with controllers. I'm not clear why
yet, but there is something in Rails' internals that make it a
challenge.

One way I've handled this successfully is to integrate_views for this
one example (in its own group) and specify that html elements from the
layout are not present in the form. It's a brittle example, but it's
only one.

Any better approaches out there?
This topic is locked and can not be replied to.