简体   繁体   中英

rspec-rails + rabl rendering views even if I don't want them to be rendered?

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM