Skip to content

Commit

Permalink
Disable negated matcher for rspec
Browse files Browse the repository at this point in the history
  • Loading branch information
coorasse committed Jul 6, 2022
1 parent 959eee1 commit e3d28fd
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## Unreleased

* Disable `expect(...).not_to be_able_to(:action_a, :action_b)` for multiple actions because it leads to ambiguity. ([@coorasse][])

## 3.4.0

* [#691](https://github.com/CanCanCommunity/cancancan/pull/691): Add two new subquery strategies: `joined_alias_exists_subquery`, `joined_alias_each_rule_as_exists_subquery`. ([@kaspernj][])
Expand Down
9 changes: 9 additions & 0 deletions lib/cancan/matchers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@
end
end

match_when_negated do |ability|
actions = args.first
if actions.is_a? Array
raise NotImplementedError, "`expect(...).not_to be_able_to(:action_a, :action_b)` is no longer supported.\nThis syntax leads to ambiguity and it has therefore been disabled.\nPlease split into separate expectations."
else
!ability.can?(*args)
end
end

# Check that RSpec is < 2.99
if !respond_to?(:failure_message) && respond_to?(:failure_message_for_should)
alias_method :failure_message, :failure_message_for_should
Expand Down
6 changes: 2 additions & 4 deletions spec/cancan/matchers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,11 @@
end

it 'delegates to can? with array of abilities with empty array' do
is_expected.not_to be_able_to([], 123)
expect { is_expected.not_to be_able_to([], 123) }.to raise_error(NotImplementedError)
end

it 'delegates to can? with array of abilities with only one eligible ability' do
is_expected.to receive(:can?).with(:read, 123) { true }
is_expected.to receive(:can?).with(:update, 123) { false }
is_expected.not_to be_able_to(%i[read update], 123)
expect { is_expected.not_to be_able_to(%i[read update], 123) }.to raise_error(NotImplementedError)
end
end
end

0 comments on commit e3d28fd

Please sign in to comment.