grape
grape copied to clipboard
Rspec Integration Test with Grape and Rails 4.2.x
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
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 :)
@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 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
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.