Grape: Rspec Integration Test with Grape and Rails 4.2.x

Created on 5 Aug 2016  路  15Comments  路  Source: ruby-grape/grape

Issue:
Your using grape, rails 4.2.x and rspec and you get an error about Array not responding to url_helpers

Fix:

    if Rails.version >= '4.2' && Rails.version < '5'
      #see actionpack/lib/action_dispatch/testing/integration.rb:184 rails 4.2.6
      #expects a rails app if resposnds to routes but we've got a grape app that responds to routes
      module ActionDispatch
        module Integration
          class Session
            def initialize(app)
              super()
              @app = app
              if app.respond_to?(:routes) && app.routes.respond_to?(:url_helpers)
                singleton_class.class_eval do
                  include app.routes.url_helpers
                  include app.routes.mounted_helpers
                end
              end
              reset!
            end
          end
        end
      end
    end

Most helpful comment

Or put them both in with a conditional on rails version so your next rails upgrade is less work.

All 15 comments

What was the issue @mrloop ?

There is an incompatibility with how ActionDispatch::Integration::Session 4.2 detects if a rails app is being run and testing Grape API with rspec integration tests, it doesn't effect 4.1 ~or 5.0~. Above the work around. It's not something to be fixed in Grape and it's already fixed in Rails. The above report and workaround are to help anybody else diagnose and fix this issue if they are on Rails 4.2.x. and using RSpec integration tests.

Edit: it does effect 5.0 but will require a different patch, see @milgner comment

@mrloop thanks for the fix, it fixed my issue as well, did you end up including this code just in your spec_helper file/individual test files or a rails initializer?

@jcope2013 it's in spec_helper file, in my case api_spec_helper.rb which is included in all the rspec integration tests exercising the grape api in my project.

@mrloop thanks

@mrloop I just encountered this with Rails 5.0.1. Do you happen to know which version contains the fix? Looking at the current master, it still seems to be unchanged?

Ha! I didn't spot that @milgner and not using Rails 5 with Grape. So it's the same issue but you'll need to patch create_session for rails 5 instead in initialize, I'm sure @dblock would appreciate a PR to fix the issue for both 4.2 and 5 :stuck_out_tongue_winking_eye:

Alright, I will prepare a patch. Already created a workaround, it's straightforward to fix.

Was this ever resolved? I'm seeing the same issue with Grape 1.0.3 and Rails 5.0.7 (yes, yes, I know)

@mrloop I'm getting the same issue, is it fixed somehow or we still have to patch it?

cc @dblock

Working on rails 5.2.1 @zauzaj @jasonl

module ActionDispatch
  module Integration
    module Runner
      def create_session(app)
        klass = APP_SESSIONS[app] ||= Class.new(Integration::Session) {
          if app.respond_to?(:routes) && app.routes.respond_to?(:url_helpers)
            include app.routes.url_helpers
            include app.routes.mounted_helpers
          end
        }
        klass.new(app)
      end
    end
  end
end

@mrloop I'm using rails 4.2.4, and it doesn't work. I guess I have to introduce patch from your first comment?

Yes try the first patch for 4.2

Or put them both in with a conditional on rails version so your next rails upgrade is less work.

@mrloop Thanks! Anyway I think this should be done in /integration by default, to avoid such confusions.

We can close it now :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dangnm picture dangnm  路  4Comments

dmmcgee picture dmmcgee  路  6Comments

Crystark picture Crystark  路  5Comments

jellybob picture jellybob  路  4Comments

dblock picture dblock  路  7Comments