Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable negated matcher for RSpec #796

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
11 changes: 11 additions & 0 deletions lib/cancan/matchers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@
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.\n" \
"This syntax leads to ambiguity and it has therefore been disabled.\n" \
'Please 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