varvet / pundit

Minimal authorization through OO design and pure Ruby classes
MIT License
8.24k stars 627 forks source link

Improve README regarding RSpec and "require" statement #675

Closed martingregoire closed 2 years ago

martingregoire commented 3 years ago

In the README file for RSpec it says:

Require pundit/rspec in your spec_helper.rb:

require "pundit/rspec"

While this works in my project (I can use the permissions helper in a policy spec file in the spec/policies/ folder), it renders a warning when running any spec:

$ rspec spec/policies/user_policy_spec.rb 
WARN: Unresolved or ambiguous specs during Gem::Specification.reset:
      minitest (~> 5.1)
      Available/installed versions of this gem:
      - 5.14.4
      - 5.13.0
WARN: Clearing out unresolved specs. Try 'gem cleanup <gem>'
Please report a bug if this causes problems.

Running gem cleanup minitest or gem cleanup does not resolve the warning.

I found this related issue: https://github.com/rubygems/rubygems/issues/1267#issuecomment-196180872

Suggestion

When moving the require call from the spec_helper.rb to the rails_helper.rb file, the warning is no longer shown.

Should the README be updated accordingly? In rails_helper.rb we see this line, which could be quoted in the README:

# Add additional requires below this line. Rails is not loaded until this point!

require 'pundit/rspec'

Environment

Some more information (not sure if relevant):

dgmstuart commented 2 years ago

Thanks for the report.

Pundit is (at least in principle) not a rails-specific gem and as far as we can see pundit/rspec doesn't depend on anything in Rails.

Pundit policies don't (by default) include anything from the Rails framework, so it should be possible to test them in isolation without loading the whole Rails application (ie. by requiring only spec_helper.rb and not rails_helper.rb).

Could the issue be that you're running rspec rather than bundle exec rspec, so you're not using the version of RSpec which you're expecting?

Alternatively this could be the result of some issue in how your spec_helper.rb or rails_helper.rb are constructed.

Have a look into that and get back to us if that doesn't solve it.

dgmstuart commented 2 years ago

Closing due to lack of response: please reopen if you have more information.

Burgestrand commented 2 years ago

Is it the case that spec_helper.rb doesn't require bundler/setup?