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
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
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
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:
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 forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.