Skip to content

Commit

Permalink
Add new RSpec/Rails/NegationBeValid cop
Browse files Browse the repository at this point in the history
  • Loading branch information
ydah committed Jul 20, 2023
1 parent 37f59b8 commit 67dcf80
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .simplecov
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

SimpleCov.start do
enable_coverage :branch
minimum_coverage line: 99.60, branch: 94.84
minimum_coverage line: 99.60, branch: 94.77
add_filter '/spec/'
add_filter '/vendor/bundle/'
end
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Master (Unreleased)

- Add new `RSpec/Rails/NegationBeValid` cop. ([@ydah])
- Fix a false negative for `RSpec/ExcessiveDocstringSpacing` when finds description with em space. ([@ydah])
- Fix a false positive for `RSpec/EmptyExampleGroup` when example group with examples defined in `if` branch inside iterator. ([@ydah])
- Update the message output of `RSpec/ExpectActual` to include the word 'value'. ([@corydiamand])
Expand Down
10 changes: 10 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1104,6 +1104,16 @@ RSpec/Rails/MinitestAssertions:
VersionAdded: '2.17'
Reference: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/Rails/MinitestAssertions

RSpec/Rails/NegationBeValid:
Description: Enforces use of `be_invalid` or `not_to` for negated be_valid.
EnforcedStyle: not_to
SupportedStyles:
- not_to
- be_invalid
Enabled: pending
VersionAdded: "<<next>>"
Reference: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/Rails/NegationBeValid

RSpec/Rails/TravelAround:
Description: Prefer to travel in `before` rather than `around`.
Enabled: pending
Expand Down
1 change: 1 addition & 0 deletions docs/modules/ROOT/pages/cops.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@
* xref:cops_rspec_rails.adoc#rspecrails/httpstatus[RSpec/Rails/HttpStatus]
* xref:cops_rspec_rails.adoc#rspecrails/inferredspectype[RSpec/Rails/InferredSpecType]
* xref:cops_rspec_rails.adoc#rspecrails/minitestassertions[RSpec/Rails/MinitestAssertions]
* xref:cops_rspec_rails.adoc#rspecrails/negationbevalid[RSpec/Rails/NegationBeValid]
* xref:cops_rspec_rails.adoc#rspecrails/travelaround[RSpec/Rails/TravelAround]

// END_COP_LIST
52 changes: 52 additions & 0 deletions docs/modules/ROOT/pages/cops_rspecrails.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,58 @@ expect(b).not_to eq(a)

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/Rails/MinitestAssertions

== RSpec/Rails/NegationBeValid

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Pending
| Yes
| Yes
| <<next>>
| -
|===

Enforces use of `be_invalid` or `not_to` for negated be_valid.

=== Examples

==== EnforcedStyle: not_to (default)

[source,ruby]
----
# bad
expect(foo).to be_invalid
# good
expect(foo).not_to be_valid
----

==== EnforcedStyle: be_invalid

[source,ruby]
----
# bad
expect(foo).not_to be_valid
# good
expect(foo).to be_invalid
----

=== Configurable attributes

|===
| Name | Default value | Configurable values

| EnforcedStyle
| `not_to`
| `not_to`, `be_invalid`
|===

=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/Rails/NegationBeValid

== RSpec/Rails/TravelAround

|===
Expand Down
57 changes: 57 additions & 0 deletions spec/rubocop/cop/rspec_rails/negation_be_valid_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::RSpec::Rails::NegationBeValid do
let(:cop_config) { { 'EnforcedStyle' => enforced_style } }

context 'with EnforcedStyle `not_to`' do
let(:enforced_style) { 'not_to' }

it 'registers an offense when using ' \
'`expect(...).to be_invalid`' do
expect_offense(<<~RUBY)
expect(foo).to be_invalid
^^^^^^^^^^^^^ Use `expect(...).not_to be_valid`.
RUBY
end

it 'does not register an offense when using ' \
'`expect(...).not_to be_valid`' do
expect_no_offenses(<<~RUBY)
expect(foo).not_to be_valid
RUBY
end

it 'does not register an offense when using ' \
'`expect(...).to be_valid`' do
expect_no_offenses(<<~RUBY)
expect(foo).to be_valid
RUBY
end
end

context 'with EnforcedStyle `be_invalid`' do
let(:enforced_style) { 'be_invalid' }

it 'registers an offense when using ' \
'`expect(...).not_to be_valid`' do
expect_offense(<<~RUBY)
expect(foo).not_to be_valid
^^^^^^^^^^^^^^^ Use `expect(...).to be_invalid`.
RUBY
end

it 'does not register an offense when using ' \
'`expect(...).to be_invalid`' do
expect_no_offenses(<<~RUBY)
expect(foo).to be_invalid
RUBY
end

it 'does not register an offense when using ' \
'`expect(...).to be_valid`' do
expect_no_offenses(<<~RUBY)
expect(foo).to be_valid
RUBY
end
end
end

0 comments on commit 67dcf80

Please sign in to comment.