ccocchi / rabl-rails

Rails 4.2+ templating system with JSON, XML and Plist support.
MIT License
209 stars 51 forks source link

Rspec - Failure/Error: render - ActionView::Template::Error #31

Closed abrisse closed 11 years ago

abrisse commented 11 years ago

I switched from rabl to rabl-rails. It works great except during the tests (rspec).

My view:

object :@media

attributes :types

My test:

describe 'v1/medias/show' do
  it 'should render a valid json' do
    render # it fails here
    expect { JSON.parse(rendered) }.to_not raise_error(JSON::ParserError)
  end
end

The error:

 Failure/Error: render
     ActionView::Template::Error:
       undefined method `start_with?' for :__memoized:Symbol
     # ./app/views/v1/medias/show.rabl:2:in `_app_views_v__medias_show_rabl___3750579331352501291_55842580'
     # ./spec/views/v1/medias_view_show_spec.rb:14:in `block (2 levels) in <top (required)>'
ccocchi commented 11 years ago

Which version on rabl-rails do you used?

abrisse commented 11 years ago

0.3.1

ccocchi commented 11 years ago

There is only one place in the code source where I used start_with? without using to_s before it when dealing with assigns in action_controller.

Which version of rails are you using? Do you use another gem that deals with controller or it is vanilla rails ?

abrisse commented 11 years ago

I am using rails 3.2.13. The problem only occurs with rspec. Is there any way to see the compiled view '_app_views_v__medias_show_rabl___3750579331352501291_55842580' ?

ccocchi commented 11 years ago

Hmm, I also use RSpec and I never ran into this issue. No the compiled view is from ActionView so I don't really know if you can see it.

But, you can try to monkey patch RablRails in you spec_helper.rb with this:

module RablRails
  module Renderers
    class Base

      private

      def setup_render_context
        @_context.instance_variable_get(:@_assigns).each_pair { |k, v|
          instance_variable_set("@#{k}", v) unless k.to_s.start_with?('_')
        }
      end
    end
  end
end

That should fix your issue, and I will include it in the next release.

abrisse commented 11 years ago

Yep it fixes the problem. Thanks!

abrisse commented 11 years ago

I close this issue but you should create an entry in the Wiki addressing this problem.

ccocchi commented 11 years ago

Added in #37