bwillis / versioncake

:cake: Version Cake is an unobtrusive way to version APIs in your Rails or Rack apps
655 stars 47 forks source link

middleware issue from rspec after a v2.0 -> v3.0 upgrade #82

Closed jobwat closed 4 years ago

jobwat commented 4 years ago

I'm upgrading an old application, the current stable is at versioncake 2.5.0, with ruby 2.3 & rails 4.2.11.1.

I can't manage to get versioncake to 3.x, the specs fail due to empty responses because of the versioned views not being loaded. But the live application is all working fine!

To my surprise, when inspecting from the controller, I can see the Rack Middleware isn't present for a rspec test.

From a development browser request:

[3] pry(#<RacesController>)> caller.grep /rack/
...
 "/Users/jobwat/.rbenv/versions/2.3.8/lib/ruby/gems/2.3.0/gems/versioncake-3.4.0/lib/versioncake/rack/middleware.rb:17:in `call'",
...

From a spec:

[1] pry(#<RacesController>)> caller.grep /rack/
=> []

I don't quite know how to debug further, hope someone can help

nht007 commented 4 years ago

Are you seeing that issue in a controller spec?

I ran into the same problem and found that it was due to versioncake's switch from Railties to Rack Middleware between v2.x and v3.x. Rails functional tests (RSpec controller tests) are not Rack middleware aware and are unable to initialize versioncake as they had in v2.x.

Looks like the solutions are to either stub request_version as suggested in the README or convert the specs to Rails integration tests (RSpec request specs) to exercise the entire stack, including the versioncake middleware

jobwat commented 4 years ago

Thank you for your comment!

I don't want to stub the request_version as I want to test a specific version is properly served from a header... but from my understanding, the middleware needs to be there to take the stubbed version anyway! no?

As for Rails integration tests, I believe that's what I'm using.

Thanks again for replying, it put my head back into it :)

bwillis commented 4 years ago

Are you sure you're using Rails integration tests and not just controller specs?

jobwat commented 4 years ago

Thanks for that insistence, I now understood the difference and indeed my tests are controller specs...

I haven't had time to get back on that project yet, but will most likely move my specs to an integration test and mock the version in the controller specs, it all makes sense now.

Thanks

bwillis commented 4 years ago

Thanks for following up @jobwat, let us know if you need any help getting setup again. 🙌