-
-
Notifications
You must be signed in to change notification settings - Fork 289
Add RSpec/NoExpectationExample
#1321
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
Changes from all commits
48ea016
17f66c2
03de7b1
c9ac59c
0370b02
7a3a0a6
f9c02de
ed7f7c0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -61,9 +61,14 @@ RSpec: | |
| Pending: | ||
| - pending | ||
| Expectations: | ||
| - are_expected | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change in Language potentially affects multiple cops (ExpectInHook, MultipleExpectations. StubbedMock). Perhaps it's worth mentioning it in the changelog. Also I'm not sure we should support the should expectations at all
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @bquorning what do you think about extending the default language?
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I haven’t personally used the ‘should’ syntax since RSpec 3 was released, but from the RSpec documentation it sounds like it’s not being removed, even in v4. So I guess we should support it? @pirj I imagine you might know about official RSpec plans?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All forms of
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Personally, I would not keep the burden of supporting
But I still see people use it, even those who joined software development less than nine years ago 😄 |
||
| - expect | ||
| - is_expected | ||
| - expect_any_instance_of | ||
| - is_expected | ||
| - should | ||
| - should_not | ||
| - should_not_receive | ||
| - should_receive | ||
| Helpers: | ||
| - let | ||
| - let! | ||
|
|
@@ -614,6 +619,13 @@ RSpec/NestedGroups: | |
| VersionChanged: '1.10' | ||
| Reference: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/NestedGroups | ||
|
|
||
| RSpec/NoExpectationExample: | ||
| Description: Checks if an example contains any expectation. | ||
| Enabled: pending | ||
| Safe: false | ||
| VersionAdded: '2.13' | ||
| Reference: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/NoExpectationExample | ||
|
|
||
| RSpec/NotToNot: | ||
| Description: Checks for consistent method usage for negating expectations. | ||
| Enabled: true | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module RuboCop | ||
| module Cop | ||
| module RSpec | ||
| # Checks if an example contains any expectation. | ||
| # | ||
| # All RSpec's example and expectation methods are covered by default. | ||
| # If you are using your own custom methods, | ||
| # add the following configuration: | ||
| # | ||
| # RSpec: | ||
| # Language: | ||
| # Examples: | ||
| # Regular: | ||
| # - custom_it | ||
| # Expectations: | ||
| # - custom_expect | ||
| # | ||
| # @example | ||
| # | ||
| # # bad | ||
| # it do | ||
| # a? | ||
| # end | ||
| # | ||
| # # good | ||
| # it do | ||
| # expect(a?).to be(true) | ||
| # end | ||
| class NoExpectationExample < Base | ||
| MSG = 'No expectation found in this example.' | ||
|
|
||
| # @!method regular_or_focused_example?(node) | ||
| # @param [RuboCop::AST::Node] node | ||
| # @return [Boolean] | ||
| def_node_matcher( | ||
| :regular_or_focused_example?, | ||
| block_pattern('{#Examples.regular | #Examples.focused}') | ||
| ) | ||
|
|
||
| # @!method including_any_expectation?(node) | ||
| # @param [RuboCop::AST::Node] node | ||
| # @return [Boolean] | ||
| def_node_search( | ||
| :including_any_expectation?, | ||
| send_pattern('#Expectations.all') | ||
| ) | ||
|
|
||
| # @param [RuboCop::AST::BlockNode] node | ||
| def on_block(node) | ||
| return unless regular_or_focused_example?(node) | ||
| return if including_any_expectation?(node) | ||
|
|
||
| add_offense(node) | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| RSpec.describe RuboCop::Cop::RSpec::NoExpectationExample do | ||
| context 'with no expectation example with it' do | ||
| it 'registers an offense' do | ||
| expect_offense(<<~RUBY) | ||
| RSpec.describe Foo do | ||
| it { bar } | ||
| ^^^^^^^^^^ No expectation found in this example. | ||
|
|
||
| it { expect(baz).to be_truthy } | ||
| end | ||
| RUBY | ||
| end | ||
| end | ||
|
|
||
| context 'with no expectation example with specify' do | ||
| it 'registers an offense' do | ||
| expect_offense(<<~RUBY) | ||
| specify { bar } | ||
| ^^^^^^^^^^^^^^^ No expectation found in this example. | ||
| RUBY | ||
| end | ||
| end | ||
|
|
||
| context 'with expectation example with should' do | ||
| it 'registers no offenses' do | ||
| expect_no_offenses(<<~RUBY) | ||
| it { should be_truthy } | ||
| RUBY | ||
| end | ||
| end | ||
|
|
||
| context 'with multi no expectation examples' do | ||
| it 'registers offenses' do | ||
| expect_offense(<<~RUBY) | ||
| it { bar } | ||
| ^^^^^^^^^^ No expectation found in this example. | ||
| it { baz } | ||
| ^^^^^^^^^^ No expectation found in this example. | ||
| RUBY | ||
| end | ||
| end | ||
|
|
||
| context 'with custom expectation example' do | ||
| it 'registers an offense' do | ||
| expect_offense(<<~RUBY) | ||
| it { custom_expect(bar) } | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^ No expectation found in this example. | ||
| RUBY | ||
| end | ||
| end | ||
|
|
||
| context 'with configured custom expectation example' do | ||
| before do | ||
| other_cops.dig('RSpec', 'Language', 'Expectations').push('custom_expect') | ||
| end | ||
|
|
||
| it 'registers no offenses' do | ||
| expect_no_offenses(<<~RUBY) | ||
| it { custom_expect(bar) } | ||
| RUBY | ||
| end | ||
| end | ||
|
|
||
| context 'with no expectation custom example' do | ||
| it 'registers no offenses' do | ||
| expect_no_offenses(<<~RUBY) | ||
| custom_it { foo } | ||
| RUBY | ||
| end | ||
| end | ||
|
|
||
| context 'with no expectation configured custom example' do | ||
| before do | ||
| other_cops.dig( | ||
| 'RSpec', | ||
| 'Language', | ||
| 'Examples', | ||
| 'Regular' | ||
| ).push('custom_it') | ||
| end | ||
|
|
||
| it 'registers an offense' do | ||
| expect_offense(<<~RUBY) | ||
| custom_it { foo } | ||
| ^^^^^^^^^^^^^^^^^ No expectation found in this example. | ||
| RUBY | ||
| end | ||
| end | ||
|
|
||
| context 'with block-less example in block' do | ||
| it 'registers no offenses' do | ||
| expect_no_offenses(<<~RUBY) | ||
| RSpec.describe Foo do | ||
| it 'not implemented' | ||
| end | ||
| RUBY | ||
| end | ||
| end | ||
|
|
||
| context 'with no expectation pending example' do | ||
| it 'registers no offenses' do | ||
| expect_no_offenses(<<~RUBY) | ||
| pending { bar } | ||
| RUBY | ||
| end | ||
| end | ||
|
|
||
| context 'with no expectation skipped example' do | ||
| it 'registers no offenses' do | ||
| expect_no_offenses(<<~RUBY) | ||
| skip { bar } | ||
| RUBY | ||
| end | ||
| end | ||
| end |
Uh oh!
There was an error while loading. Please reload this page.