diff --git a/CHANGELOG.md b/CHANGELOG.md index b16cddb..731a95c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## Unreleased +### Changed + +- Using `permit` matcher without a surrouding `permissions` block now raises a useful error. (#836) + ## 2.4.0 (2024-08-26) ## Changed diff --git a/lib/pundit/rspec.rb b/lib/pundit/rspec.rb index fb44ba6..192d5c8 100644 --- a/lib/pundit/rspec.rb +++ b/lib/pundit/rspec.rb @@ -93,7 +93,11 @@ def current_example end def permissions - current_example.metadata[:permissions] + current_example.metadata.fetch(:permissions) do + raise KeyError, <<~ERROR.strip + No permissions in example metadata, did you forget to wrap with `permissions :show?, ...`? + ERROR + end end end # rubocop:enable Metrics/BlockLength diff --git a/spec/rspec_dsl_spec.rb b/spec/rspec_dsl_spec.rb index c1f65da..45fd630 100644 --- a/spec/rspec_dsl_spec.rb +++ b/spec/rspec_dsl_spec.rb @@ -36,6 +36,16 @@ end describe "#permit" do + context "when not appropriately wrapped in permissions" do + it "raises a descriptive error" do + expect do + expect(policy).to permit(user, post) + end.to raise_error(KeyError, <<~MSG.strip) + No permissions in example metadata, did you forget to wrap with `permissions :show?, ...`? + MSG + end + end + permissions :edit?, :update? do it "succeeds when action is permitted" do expect(policy).to permit(user, post)