grape icon indicating copy to clipboard operation
grape copied to clipboard

Rspec Integration Test with Grape and Rails 4.2.x

Open mrloop opened this issue 8 years ago • 18 comments

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

mrloop avatar Aug 05 '16 18:08 mrloop

What was the issue @mrloop ?

dblock avatar Aug 05 '16 23:08 dblock

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 avatar Aug 06 '16 04:08 mrloop

@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 avatar Aug 19 '16 15:08 jcope2013

@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 avatar Aug 19 '16 15:08 mrloop

@mrloop thanks

jcope2013 avatar Aug 19 '16 15:08 jcope2013

@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?

milgner avatar Jan 31 '17 14:01 milgner

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:

mrloop avatar Jan 31 '17 19:01 mrloop

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

milgner avatar Jan 31 '17 21:01 milgner

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

jasonl avatar Jun 26 '18 13:06 jasonl

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

cc @dblock

zauzaj avatar Apr 24 '19 13:04 zauzaj

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 avatar Apr 24 '19 13:04 mrloop

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

zauzaj avatar Apr 24 '19 13:04 zauzaj

Yes try the first patch for 4.2

mrloop avatar Apr 24 '19 13:04 mrloop

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

mrloop avatar Apr 24 '19 13:04 mrloop

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

We can close it now :)

zauzaj avatar Apr 24 '19 13:04 zauzaj

@mrloop I'm sorry, but do you remember where this patch code goes? I tried dropping the Rails 5 version (using 5.2.8.1, grape version 1.6.2) into my rails_helper.rb, then found and replaced the create_session method in the actionpack gem itself (at .../gems/actionpack-5.2.8.1/lib/action_dispatch/testing/integration.rb). Neither worked.

jklemon17 avatar Nov 03 '22 15:11 jklemon17

@jklemon17 eck, this is an old bug, not working with Grape or Ruby just now, don't remember and don't have access to the codebase it was used in to check :( good luck patching it

mrloop avatar Nov 03 '22 15:11 mrloop

It would be amazing if someone took the time to contribute to https://github.com/ruby-grape/grape-on-rails (upgrade it, make versions for various versions of Rails, etc.). I'd be happy to help with specific issues like these given visibly failing tests.

dblock avatar Nov 03 '22 16:11 dblock