2

I'm using rabl 0.6.13, rspec-rails 2.10.1, and rails 3.2.6.

I'm trying to spec my controllers in isolation, but for some reason my rabl templates are throwing me all sorts of undefined method exceptions on the mocks I use in my controller specs. I am not using render_views. I thought rspec did not process views unless you specify render_views in the controller spec. I've run debugger to ensure that render_views? evaluates to false in the before block rspec-rails inserts. Has anyone else run into this problem?

Aaron Gibralter
  • 4,773
  • 3
  • 35
  • 50
  • Also, this doesn't happen with my erb/haml templates. – Aaron Gibralter Jun 20 '12 at 17:54
  • Have you open an issue on rspec-rails project ? if not, why ? – shingara Jul 04 '12 at 10:26
  • Yes, in my answer below I posted the link: https://github.com/rspec/rspec-rails/issues/565#issuecomment-6479742 As you can see from the conversation with David Chelimsky, we decided that template engines (like Rabl) should be able to deal with a blank `template.source`. David Sommers said that Rabl would remove the `if template.source.empty?` part of `self.call(template)` (see below). In fact, it's already been committed: https://github.com/nesquena/rabl/commit/e7be8bea95ea12603d7b9581330020b2589a683d. – Aaron Gibralter Jul 04 '12 at 14:03

1 Answers1

0

Ah, so I figured it out! It's outlined in these comments: - https://github.com/nesquena/rabl/issues/37#issuecomment-6474467 - https://github.com/rspec/rspec-rails/issues/565#issuecomment-6474362

def self.call(template)
  source = if template.source.empty?
    File.read(template.identifier)
  else # use source
    template.source
  end

  %{ ::Rabl::Engine.new(#{source.inspect}).
      render(self, assigns.merge(local_assigns)) }
end # call

rspec-rails stubs out templates to have a blank source (rather than stubbing the whole rendering process so rails properly handles formats/mime-types/etc.), and the rabl handler sees the blanks source and decides to read the file-system. So either rabl or rspec-rails will need a slight tweak to get this working. For now I've monkey patched rspec-rails:

class EmptyTemplatePathSetDecorator < ::ActionView::Resolver
  attr_reader :original_path_set

  def initialize(original_path_set)
    @original_path_set = original_path_set
  end

  # @api private
  def find_all(*args)
    original_path_set.find_all(*args).collect do |template|
      ::ActionView::Template.new(
        " ", # <======================== this is not "empty"
        template.identifier,
        template.handler,
        {
          :virtual_path => template.virtual_path,
          :format => template.formats
        }
      )
    end
  end
end
Aaron Gibralter
  • 4,773
  • 3
  • 35
  • 50