Skip to content

Commit

Permalink
Add a more useful error for when permissions is forgotten in rspec
Browse files Browse the repository at this point in the history
  • Loading branch information
Burgestrand committed Oct 11, 2024
1 parent e296e83 commit 6451ee7
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion lib/pundit/rspec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions spec/rspec_dsl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 6451ee7

Please sign in to comment.