diff --git a/CHANGELOG.md b/CHANGELOG.md index 956dffe76..290ecaf11 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Master (Unreleased) +- Support asserts with messages in `Rspec/BeEmpty`. ([@G-Rath]) - Add support for `assert_empty`, `assert_not_empty` and `refute_empty` to `RSpec/Rails/MinitestAssertions`. ([@ydah]) - Support correcting `*_match` assertions in `RSpec/Rails/MinitestAssertions`. ([@G-Rath]) - Support correcting `*_instance_of` assertions in `RSpec/Rails/MinitestAssertions`. ([@G-Rath]) diff --git a/lib/rubocop/cop/rspec/be_empty.rb b/lib/rubocop/cop/rspec/be_empty.rb index 68d3cd69d..fbd79fdb0 100644 --- a/lib/rubocop/cop/rspec/be_empty.rb +++ b/lib/rubocop/cop/rspec/be_empty.rb @@ -28,6 +28,7 @@ class BeEmpty < Base (send nil? :match_array (array)) (send nil? :contain_exactly) } + _? ) PATTERN diff --git a/spec/rubocop/cop/rspec/be_empty_spec.rb b/spec/rubocop/cop/rspec/be_empty_spec.rb index c3cc51dd4..58c6cee12 100644 --- a/spec/rubocop/cop/rspec/be_empty_spec.rb +++ b/spec/rubocop/cop/rspec/be_empty_spec.rb @@ -12,6 +12,18 @@ RUBY end + it 'registers an offense when ' \ + 'using `expect(array).to contain_exactly, "with a message"`' do + expect_offense(<<~RUBY) + expect(array).to contain_exactly, "with a message" + ^^^^^^^^^^^^^^^ Use `be_empty` matchers for checking an empty array. + RUBY + + expect_correction(<<~RUBY) + expect(array).to be_empty, "with a message" + RUBY + end + it 'registers an offense when using `expect(array).to match_array([])`' do expect_offense(<<~RUBY) expect(array).to match_array([]) @@ -23,9 +35,28 @@ RUBY end + it 'registers an offense when ' \ + 'using `expect(array).to match_array([]), "with a message"`' do + expect_offense(<<~RUBY) + expect(array).to match_array([]), "with a message" + ^^^^^^^^^^^^^^^ Use `be_empty` matchers for checking an empty array. + RUBY + + expect_correction(<<~RUBY) + expect(array).to be_empty, "with a message" + RUBY + end + it 'does not register an offense when using `expect(array).to be_empty`' do expect_no_offenses(<<~RUBY) expect(array).to be_empty RUBY end + + it 'does not register an offense when ' \ + 'using `expect(array).to be_empty, "with a message"`' do + expect_no_offenses(<<~RUBY) + expect(array).to be_empty, "with a message" + RUBY + end end