Skip to content

Commit e3d28fd

Browse files
committed
Disable negated matcher for rspec
1 parent 959eee1 commit e3d28fd

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
## Unreleased
22

3+
* Disable `expect(...).not_to be_able_to(:action_a, :action_b)` for multiple actions because it leads to ambiguity. ([@coorasse][])
4+
35
## 3.4.0
46

57
* [#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][])

lib/cancan/matchers.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@
2323
end
2424
end
2525

26+
match_when_negated do |ability|
27+
actions = args.first
28+
if actions.is_a? Array
29+
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."
30+
else
31+
!ability.can?(*args)
32+
end
33+
end
34+
2635
# Check that RSpec is < 2.99
2736
if !respond_to?(:failure_message) && respond_to?(:failure_message_for_should)
2837
alias_method :failure_message, :failure_message_for_should

spec/cancan/matchers_spec.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,11 @@
4646
end
4747

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

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

0 commit comments

Comments
 (0)