{"record":{"id":"437ccccb9a0350a1","repo":"varvet/pundit","slug":"no-permissions-in-example-metadata-did-you-forget","errorCode":null,"errorMessage":"No permissions in example metadata, did you forget to wrap with `permissions :show?, ...`?","messagePattern":"No permissions in example metadata, did you forget to wrap with `permissions :show\\?, \\.\\.\\.`\\?","errorType":"exception","errorClass":"KeyError","httpStatus":null,"severity":"error","filePath":"lib/pundit/rspec.rb","lineNumber":104,"sourceCode":"          # :nocov:\n        end\n\n        if ::RSpec.respond_to?(:current_example)\n          def current_example\n            ::RSpec.current_example\n          end\n        else\n          # :nocov:\n          # Compatibility with RSpec < 3.0, released 2014-06-01.\n          def current_example\n            example\n          end\n          # :nocov:\n        end\n\n        def permissions\n          current_example.metadata.fetch(:permissions) do\n            raise KeyError, <<~ERROR.strip\n              No permissions in example metadata, did you forget to wrap with `permissions :show?, ...`?\n            ERROR\n          end\n        end\n      end\n      # rubocop:enable Metrics/BlockLength\n    end\n\n    # Mixed in to all policy example groups to provide a DSL.\n    module DSL\n      # @example\n      #   describe PostPolicy do\n      #     permissions :show?, :update? do\n      #       it { is_expected.to permit(user, own_post) }\n      #     end\n      #   end\n      #\n      # @example focused example group","sourceCodeStart":86,"sourceCodeEnd":122,"githubUrl":"https://github.com/varvet/pundit/blob/06318683c960066a2e499341cb372e0ff4540334/lib/pundit/rspec.rb#L86-L122","documentation":"Pundit's RSpec `permit` matcher reads the permission list from the current example's metadata, which is only populated by the `permissions :show?, ... do ... end` block from Pundit::RSpec::DSL (rspec.rb:131 stashes `metadata[:permissions]`). Calling the matcher from a plain `it`/`describe` block leaves the key absent, so `metadata.fetch(:permissions)` raises KeyError with this hint.","triggerScenarios":"Writing `describe PostPolicy do it { is_expected.to permit(user, post) } end` (no permissions wrapper); moving a `permit` expectation into a shared example or helper method that runs outside a permissions block; using `specify`/custom example groups without the DSL wrapper; keeping specs written against an older style where permissions were inferred rather than declared.","commonSituations":"Copy-pasting policy specs from blog posts or generators that predate the permissions DSL; refactoring policy specs into shared contexts and losing the wrapper; spec files under spec/policies (or with `type: :policy`) so the DSL is loaded, but examples structured as plain `it` blocks.","solutions":["Wrap the example in a permissions block: `permissions :show? do it { is_expected.to permit(user, post) } end`.","List every predicate the matcher should exercise: `permissions :show?, :update? do ... end` — one failing permission is reported in the matcher's failure message.","Keep the spec under spec/policies or tagged `type: :policy` so Pundit::RSpec::PolicyExampleGroup (and thus the DSL) is included in the first place.","To focus a run, append the symbol: `permissions :show?, :focus do ... end` — the DSL strips it and sets focus metadata."],"exampleFix":"# before\ndescribe PostPolicy do\n  it { is_expected.to permit(user, post) } # KeyError: no :permissions metadata\nend\n\n# after\ndescribe PostPolicy do\n  permissions :show?, :update? do\n    it { is_expected.to permit(user, post) }\n  end\nend","handlingStrategy":"validation","validationCode":"# Inside a policy spec, guard before invoking the matcher:\nunless defined?(RSpec.current_example) && RSpec.current_example.metadata.key?(:permissions)\n  raise ArgumentError, \"wrap examples in `permissions :show?, ... do ... end` before using permit\"\nend","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Standardize policy specs on the DSL shape: `permissions :verb? do it { is_expected.to permit(user, record) } end`.","Add a rubocop-rspec custom rule or shared example that fails when a `permit` matcher appears in an example group without a :permissions metadata ancestor.","Keep policy specs in spec/policies (or tag them `type: :policy`) so Pundit's DSL and matchers are always loaded where they are used."],"tags":["ruby","pundit","rspec","testing","metadata","key-error","matcher"],"backgroundTag":"missing-test-metadata","analyzedSha":"06318683c960066a2e499341cb372e0ff4540334","analyzedAt":"2026-08-21T18:13:24.520Z","schemaVersion":2},"datasetVersion":"2026-08-21T18:17:14.833Z"}