Test nested layout helper with RSpec and Haml

haml, rspec, ruby, ruby-on-rails

Solution

You cannot use render-calls directly in rspecs for helpers, as the view-path is not set in that context.

Suggestions:

- use a mock for the render-call.

- change your code, and move your render-call out into the view-template, then only return the arguments for the render-call from the inside_layout method.

Read more: http://pivotallabs.com/users/john/blog/articles/854-friday-5-11-standup-view-paths-in-rspec-helper-tests-write-attribute-for-type-inference-

Problem

In ApplicationHelper I have such code: ``` def inside_layout layout = 'application', &block @template.instance_variable_set '@content_for_layout', capture(&block) concat \ @template.render :file => "layouts/#{layout}", :use_full_path => true end ``` which behaves like this: application.html.haml: ``` !!! %html %head ... %body = yield ``` common_pages.html.haml: ``` - inside_layout do ... ``` Then common_pages layout render within application layout. How I can test this helper with RSpec? When I call inside_layout from spec file: ``` helper.inside_layout { } ``` RSpec says error: ``` ActionView::MissingTemplate in 'ApplicationHelper inside_layout should render nested layout within application layout' Missing layout layouts/application.erb in view path ``` But application works fine.

Original source