[rspec-users] How to expect no layout

David Chelimsky dchelimsky at gmail.com
Tue Nov 11 08:56:24 EST 2008


On Tue, Nov 11, 2008 at 4:46 AM, Fernando Perez <lists at ruby-forum.com> wrote:
> Nick Hoffman 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})

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?


More information about the rspec-users mailing list