rubocop-rspec 1.32.0 → 1.33.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (162) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +11 -0
  3. data/Gemfile +2 -0
  4. data/Rakefile +2 -0
  5. data/config/default.yml +6 -0
  6. data/lib/rubocop-rspec.rb +3 -0
  7. data/lib/rubocop/cop/rspec/align_left_let_brace.rb +1 -1
  8. data/lib/rubocop/cop/rspec/align_right_let_brace.rb +1 -1
  9. data/lib/rubocop/cop/rspec/any_instance.rb +3 -1
  10. data/lib/rubocop/cop/rspec/around_block.rb +4 -2
  11. data/lib/rubocop/cop/rspec/be.rb +4 -2
  12. data/lib/rubocop/cop/rspec/be_eql.rb +3 -1
  13. data/lib/rubocop/cop/rspec/before_after_all.rb +1 -1
  14. data/lib/rubocop/cop/rspec/capybara/current_path_expectation.rb +5 -3
  15. data/lib/rubocop/cop/rspec/capybara/feature_methods.rb +3 -3
  16. data/lib/rubocop/cop/rspec/context_wording.rb +2 -2
  17. data/lib/rubocop/cop/rspec/describe_class.rb +4 -4
  18. data/lib/rubocop/cop/rspec/describe_method.rb +2 -2
  19. data/lib/rubocop/cop/rspec/describe_symbol.rb +2 -2
  20. data/lib/rubocop/cop/rspec/described_class.rb +3 -3
  21. data/lib/rubocop/cop/rspec/dialect.rb +76 -0
  22. data/lib/rubocop/cop/rspec/empty_example_group.rb +1 -1
  23. data/lib/rubocop/cop/rspec/empty_line_after_example_group.rb +1 -1
  24. data/lib/rubocop/cop/rspec/empty_line_after_final_let.rb +1 -1
  25. data/lib/rubocop/cop/rspec/empty_line_after_hook.rb +1 -1
  26. data/lib/rubocop/cop/rspec/empty_line_after_subject.rb +1 -1
  27. data/lib/rubocop/cop/rspec/example_length.rb +1 -1
  28. data/lib/rubocop/cop/rspec/example_without_description.rb +2 -2
  29. data/lib/rubocop/cop/rspec/example_wording.rb +2 -2
  30. data/lib/rubocop/cop/rspec/expect_actual.rb +1 -1
  31. data/lib/rubocop/cop/rspec/expect_change.rb +2 -2
  32. data/lib/rubocop/cop/rspec/expect_in_hook.rb +1 -1
  33. data/lib/rubocop/cop/rspec/expect_output.rb +1 -1
  34. data/lib/rubocop/cop/rspec/factory_bot/attribute_defined_statically.rb +21 -40
  35. data/lib/rubocop/cop/rspec/factory_bot/create_list.rb +2 -2
  36. data/lib/rubocop/cop/rspec/file_path.rb +2 -2
  37. data/lib/rubocop/cop/rspec/focus.rb +3 -3
  38. data/lib/rubocop/cop/rspec/hook_argument.rb +4 -6
  39. data/lib/rubocop/cop/rspec/hooks_before_examples.rb +2 -5
  40. data/lib/rubocop/cop/rspec/implicit_expect.rb +2 -2
  41. data/lib/rubocop/cop/rspec/implicit_subject.rb +1 -1
  42. data/lib/rubocop/cop/rspec/instance_spy.rb +1 -1
  43. data/lib/rubocop/cop/rspec/instance_variable.rb +1 -1
  44. data/lib/rubocop/cop/rspec/invalid_predicate_matcher.rb +4 -2
  45. data/lib/rubocop/cop/rspec/it_behaves_like.rb +1 -1
  46. data/lib/rubocop/cop/rspec/iterated_expectation.rb +3 -1
  47. data/lib/rubocop/cop/rspec/leading_subject.rb +1 -2
  48. data/lib/rubocop/cop/rspec/let_before_examples.rb +2 -5
  49. data/lib/rubocop/cop/rspec/let_setup.rb +1 -1
  50. data/lib/rubocop/cop/rspec/message_chain.rb +3 -1
  51. data/lib/rubocop/cop/rspec/message_expectation.rb +1 -1
  52. data/lib/rubocop/cop/rspec/message_spies.rb +3 -4
  53. data/lib/rubocop/cop/rspec/missing_example_group_argument.rb +1 -1
  54. data/lib/rubocop/cop/rspec/multiple_describes.rb +1 -1
  55. data/lib/rubocop/cop/rspec/multiple_expectations.rb +1 -1
  56. data/lib/rubocop/cop/rspec/multiple_subjects.rb +6 -2
  57. data/lib/rubocop/cop/rspec/named_subject.rb +1 -1
  58. data/lib/rubocop/cop/rspec/nested_groups.rb +3 -4
  59. data/lib/rubocop/cop/rspec/not_to_not.rb +3 -1
  60. data/lib/rubocop/cop/rspec/overwriting_setup.rb +1 -1
  61. data/lib/rubocop/cop/rspec/pending.rb +3 -3
  62. data/lib/rubocop/cop/rspec/predicate_matcher.rb +17 -14
  63. data/lib/rubocop/cop/rspec/rails/http_status.rb +2 -2
  64. data/lib/rubocop/cop/rspec/receive_counts.rb +5 -6
  65. data/lib/rubocop/cop/rspec/receive_never.rb +4 -7
  66. data/lib/rubocop/cop/rspec/repeated_description.rb +3 -1
  67. data/lib/rubocop/cop/rspec/repeated_example.rb +3 -1
  68. data/lib/rubocop/cop/rspec/return_from_stub.rb +2 -2
  69. data/lib/rubocop/cop/rspec/scattered_let.rb +1 -1
  70. data/lib/rubocop/cop/rspec/scattered_setup.rb +1 -1
  71. data/lib/rubocop/cop/rspec/shared_context.rb +2 -2
  72. data/lib/rubocop/cop/rspec/shared_examples.rb +7 -7
  73. data/lib/rubocop/cop/rspec/single_argument_message_chain.rb +3 -1
  74. data/lib/rubocop/cop/rspec/subject_stub.rb +1 -1
  75. data/lib/rubocop/cop/rspec/unspecified_exception.rb +5 -3
  76. data/lib/rubocop/cop/rspec/verified_doubles.rb +2 -2
  77. data/lib/rubocop/cop/rspec/void_expect.rb +1 -1
  78. data/lib/rubocop/cop/rspec/yield.rb +2 -4
  79. data/lib/rubocop/cop/rspec_cops.rb +3 -0
  80. data/lib/rubocop/rspec.rb +2 -0
  81. data/lib/rubocop/rspec/blank_line_separation.rb +3 -1
  82. data/lib/rubocop/rspec/config_formatter.rb +3 -1
  83. data/lib/rubocop/rspec/description_extractor.rb +3 -1
  84. data/lib/rubocop/rspec/factory_bot.rb +58 -0
  85. data/lib/rubocop/rspec/final_end_location.rb +2 -0
  86. data/lib/rubocop/rspec/inject.rb +2 -0
  87. data/lib/rubocop/rspec/language.rb +9 -2
  88. data/lib/rubocop/rspec/top_level_describe.rb +2 -0
  89. data/lib/rubocop/rspec/version.rb +1 -1
  90. data/rubocop-rspec.gemspec +3 -1
  91. data/spec/project/changelog_spec.rb +2 -0
  92. data/spec/project/default_config_spec.rb +2 -0
  93. data/spec/project/project_requires_spec.rb +2 -0
  94. data/spec/rubocop/cop/rspec/align_left_let_brace_spec.rb +22 -37
  95. data/spec/rubocop/cop/rspec/align_right_let_brace_spec.rb +21 -36
  96. data/spec/rubocop/cop/rspec/any_instance_spec.rb +2 -0
  97. data/spec/rubocop/cop/rspec/around_block_spec.rb +2 -0
  98. data/spec/rubocop/cop/rspec/be_eql_spec.rb +25 -4
  99. data/spec/rubocop/cop/rspec/be_spec.rb +2 -0
  100. data/spec/rubocop/cop/rspec/before_after_all_spec.rb +2 -0
  101. data/spec/rubocop/cop/rspec/capybara/current_path_expectation_spec.rb +2 -0
  102. data/spec/rubocop/cop/rspec/capybara/feature_methods_spec.rb +2 -0
  103. data/spec/rubocop/cop/rspec/context_wording_spec.rb +32 -0
  104. data/spec/rubocop/cop/rspec/describe_class_spec.rb +2 -0
  105. data/spec/rubocop/cop/rspec/describe_method_spec.rb +2 -0
  106. data/spec/rubocop/cop/rspec/describe_symbol_spec.rb +2 -0
  107. data/spec/rubocop/cop/rspec/described_class_spec.rb +4 -1
  108. data/spec/rubocop/cop/rspec/dialect_spec.rb +78 -0
  109. data/spec/rubocop/cop/rspec/empty_line_after_example_group_spec.rb +40 -21
  110. data/spec/rubocop/cop/rspec/empty_line_after_final_let_spec.rb +77 -69
  111. data/spec/rubocop/cop/rspec/empty_line_after_hook_spec.rb +24 -19
  112. data/spec/rubocop/cop/rspec/empty_line_after_subject_spec.rb +16 -19
  113. data/spec/rubocop/cop/rspec/example_length_spec.rb +2 -0
  114. data/spec/rubocop/cop/rspec/example_without_description_spec.rb +2 -0
  115. data/spec/rubocop/cop/rspec/example_wording_spec.rb +45 -24
  116. data/spec/rubocop/cop/rspec/expect_change_spec.rb +22 -14
  117. data/spec/rubocop/cop/rspec/factory_bot/attribute_defined_statically_spec.rb +36 -0
  118. data/spec/rubocop/cop/rspec/factory_bot/create_list_spec.rb +2 -0
  119. data/spec/rubocop/cop/rspec/file_path_spec.rb +2 -0
  120. data/spec/rubocop/cop/rspec/focus_spec.rb +2 -2
  121. data/spec/rubocop/cop/rspec/hook_argument_spec.rb +2 -0
  122. data/spec/rubocop/cop/rspec/hooks_before_examples_spec.rb +47 -32
  123. data/spec/rubocop/cop/rspec/implicit_expect_spec.rb +20 -19
  124. data/spec/rubocop/cop/rspec/implicit_subject_spec.rb +56 -68
  125. data/spec/rubocop/cop/rspec/instance_spy_spec.rb +16 -15
  126. data/spec/rubocop/cop/rspec/instance_variable_spec.rb +2 -0
  127. data/spec/rubocop/cop/rspec/invalid_predicate_matcher_spec.rb +2 -0
  128. data/spec/rubocop/cop/rspec/it_behaves_like_spec.rb +10 -12
  129. data/spec/rubocop/cop/rspec/iterated_expectation_spec.rb +2 -0
  130. data/spec/rubocop/cop/rspec/leading_subject_spec.rb +31 -47
  131. data/spec/rubocop/cop/rspec/let_before_examples_spec.rb +27 -26
  132. data/spec/rubocop/cop/rspec/message_chain_spec.rb +2 -0
  133. data/spec/rubocop/cop/rspec/missing_example_group_argument_spec.rb +2 -0
  134. data/spec/rubocop/cop/rspec/multiple_describes_spec.rb +2 -0
  135. data/spec/rubocop/cop/rspec/multiple_subjects_spec.rb +24 -37
  136. data/spec/rubocop/cop/rspec/not_to_not_spec.rb +22 -32
  137. data/spec/rubocop/cop/rspec/overwriting_setup_spec.rb +2 -0
  138. data/spec/rubocop/cop/rspec/pending_spec.rb +2 -0
  139. data/spec/rubocop/cop/rspec/predicate_matcher_spec.rb +2 -0
  140. data/spec/rubocop/cop/rspec/receive_counts_spec.rb +47 -8
  141. data/spec/rubocop/cop/rspec/receive_never_spec.rb +16 -4
  142. data/spec/rubocop/cop/rspec/return_from_stub_spec.rb +95 -72
  143. data/spec/rubocop/cop/rspec/scattered_let_spec.rb +2 -0
  144. data/spec/rubocop/cop/rspec/scattered_setup_spec.rb +2 -0
  145. data/spec/rubocop/cop/rspec/shared_context_spec.rb +29 -34
  146. data/spec/rubocop/cop/rspec/shared_examples_spec.rb +16 -34
  147. data/spec/rubocop/cop/rspec/single_argument_message_chain_spec.rb +44 -48
  148. data/spec/rubocop/cop/rspec/unspecified_exception_spec.rb +2 -0
  149. data/spec/rubocop/cop/rspec/verified_doubles_spec.rb +2 -0
  150. data/spec/rubocop/cop/rspec/void_expect_spec.rb +2 -0
  151. data/spec/rubocop/cop/rspec/yield_spec.rb +22 -21
  152. data/spec/rubocop/rspec/config_formatter_spec.rb +2 -0
  153. data/spec/rubocop/rspec/description_extractor_spec.rb +2 -0
  154. data/spec/rubocop/rspec/language/selector_set_spec.rb +2 -0
  155. data/spec/rubocop/rspec/util/one_spec.rb +2 -0
  156. data/spec/rubocop/rspec/wording_spec.rb +2 -0
  157. data/spec/shared/autocorrect_behavior.rb +2 -0
  158. data/spec/shared/detects_style_behavior.rb +2 -0
  159. data/spec/shared/smoke_test_examples.rb +2 -0
  160. data/spec/spec_helper.rb +2 -0
  161. data/spec/support/expect_offense.rb +3 -1
  162. metadata +8 -4
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  RSpec.describe RuboCop::Cop::RSpec::LetBeforeExamples do
2
4
  subject(:cop) { described_class.new }
3
5
 
@@ -9,6 +11,13 @@ RSpec.describe RuboCop::Cop::RSpec::LetBeforeExamples do
9
11
  ^^^^^^^^^^^^^^^^^ Move `let` before the examples in the group.
10
12
  end
11
13
  RUBY
14
+
15
+ expect_correction(<<-RUBY)
16
+ RSpec.describe User do
17
+ let(:foo) { bar }
18
+ it { is_expected.to be_after_let }
19
+ end
20
+ RUBY
12
21
  end
13
22
 
14
23
  it 'flags `let` after `context`' do
@@ -22,6 +31,16 @@ RSpec.describe RuboCop::Cop::RSpec::LetBeforeExamples do
22
31
  ^^^^^^^^^^^^^^^^^ Move `let` before the examples in the group.
23
32
  end
24
33
  RUBY
34
+
35
+ expect_correction(<<-RUBY)
36
+ RSpec.describe User do
37
+ let(:foo) { bar }
38
+ context 'a context' do
39
+ it { is_expected.to be_after_let }
40
+ end
41
+
42
+ end
43
+ RUBY
25
44
  end
26
45
 
27
46
  it 'flags `let` after `include_examples`' do
@@ -33,6 +52,14 @@ RSpec.describe RuboCop::Cop::RSpec::LetBeforeExamples do
33
52
  ^^^^^^^^^^^^^^^^^ Move `let` before the examples in the group.
34
53
  end
35
54
  RUBY
55
+
56
+ expect_correction(<<-RUBY)
57
+ RSpec.describe User do
58
+ let(:foo) { bar }
59
+ include_examples('should be after let')
60
+
61
+ end
62
+ RUBY
36
63
  end
37
64
 
38
65
  it 'does not flag `let` before the examples' do
@@ -86,32 +113,6 @@ RSpec.describe RuboCop::Cop::RSpec::LetBeforeExamples do
86
113
  RUBY
87
114
  end
88
115
 
89
- bad_code = <<-RUBY
90
- RSpec.describe User do
91
- include_examples('should be after let')
92
- context 'another one' do
93
- let(:foo) { baz }
94
- include_examples('should be ok')
95
- end
96
-
97
- let(:foo) { bar }
98
- end
99
- RUBY
100
-
101
- good_code = <<-RUBY
102
- RSpec.describe User do
103
- let(:foo) { bar }
104
- include_examples('should be after let')
105
- context 'another one' do
106
- let(:foo) { baz }
107
- include_examples('should be ok')
108
- end
109
-
110
- end
111
- RUBY
112
-
113
- include_examples 'autocorrect', bad_code, good_code
114
-
115
116
  bad_code = <<-RUBY
116
117
  RSpec.describe User do
117
118
  include_examples('should be after let')
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  RSpec.describe RuboCop::Cop::RSpec::MessageChain do
2
4
  subject(:cop) { described_class.new }
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  RSpec.describe RuboCop::Cop::RSpec::MissingExampleGroupArgument do
2
4
  subject(:cop) { described_class.new }
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  RSpec.describe RuboCop::Cop::RSpec::MultipleDescribes do
2
4
  subject(:cop) { described_class.new }
3
5
 
@@ -19,17 +19,31 @@ RSpec.describe RuboCop::Cop::RSpec::MultipleSubjects do
19
19
  end
20
20
  end
21
21
  RUBY
22
+
23
+ expect_correction(<<-RUBY)
24
+ describe 'hello there' do
25
+ let(:foo) { 1 }
26
+ let(:bar) { 2 }
27
+ subject(:baz) { 4 }
28
+
29
+ describe 'baz' do
30
+ subject(:norf) { 1 }
31
+ end
32
+ end
33
+ RUBY
22
34
  end
23
35
 
24
36
  it 'does not try to autocorrect subject!' do
25
37
  source = <<-RUBY
26
38
  describe Foo do
27
39
  subject! { a }
40
+ ^^^^^^^^^^^^^^ Do not set more than one subject per example group
28
41
  subject! { b }
29
42
  end
30
43
  RUBY
31
44
 
32
- expect(autocorrect_source(source, 'example_spec.rb')).to eql(source)
45
+ expect_offense(source)
46
+ expect_no_corrections
33
47
  end
34
48
 
35
49
  it 'does not flag shared example groups' do
@@ -50,47 +64,20 @@ RSpec.describe RuboCop::Cop::RSpec::MultipleSubjects do
50
64
  RUBY
51
65
  end
52
66
 
53
- include_examples(
54
- 'autocorrect',
55
- <<-RUBY,
56
- describe 'hello there' do
57
- subject(:foo) { 1 }
58
- subject(:bar) { 2 }
59
- subject(:baz) { 3 }
60
-
61
- describe 'baz' do
62
- subject(:norf) { 1 }
63
- end
64
- end
65
- RUBY
66
- <<-RUBY
67
+ it 'autocorrects' do
68
+ expect_offense(<<-RUBY)
67
69
  describe 'hello there' do
68
- let(:foo) { 1 }
69
- let(:bar) { 2 }
70
- subject(:baz) { 3 }
71
-
72
- describe 'baz' do
73
- subject(:norf) { 1 }
74
- end
70
+ subject { 1 }
71
+ ^^^^^^^^^^^^^ Do not set more than one subject per example group
72
+ subject { 2 }
73
+ ^^^^^^^^^^^^^ Do not set more than one subject per example group
74
+ subject { 3 }
75
75
  end
76
76
  RUBY
77
- )
78
-
79
- include_examples(
80
- 'autocorrect',
81
- <<-RUBY.strip_indent.chomp,
77
+ expect_correction(<<-RUBY)
82
78
  describe 'hello there' do
83
- subject { 1 }
84
- subject { 2 }
85
79
  subject { 3 }
86
80
  end
87
81
  RUBY
88
- [
89
- "describe 'hello there' do",
90
- ' ',
91
- ' ',
92
- ' subject { 3 }',
93
- 'end'
94
- ].join("\n")
95
- )
82
+ end
96
83
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  RSpec.describe RuboCop::Cop::RSpec::NotToNot, :config do
2
4
  subject(:cop) { described_class.new(config) }
3
5
 
@@ -9,6 +11,10 @@ RSpec.describe RuboCop::Cop::RSpec::NotToNot, :config do
9
11
  it { expect(false).to_not be_true }
10
12
  ^^^^^^ Prefer `not_to` over `to_not`.
11
13
  RUBY
14
+
15
+ expect_correction(<<-RUBY)
16
+ it { expect(false).not_to be_true }
17
+ RUBY
12
18
  end
13
19
 
14
20
  it 'detects the `to_not` offense on an expect block' do
@@ -18,6 +24,12 @@ RSpec.describe RuboCop::Cop::RSpec::NotToNot, :config do
18
24
  }.to_not raise_error
19
25
  ^^^^^^ Prefer `not_to` over `to_not`.
20
26
  RUBY
27
+
28
+ expect_correction(<<-RUBY)
29
+ expect {
30
+ 2 + 2
31
+ }.not_to raise_error
32
+ RUBY
21
33
  end
22
34
 
23
35
  it 'detects no offense when using `not_to`' do
@@ -25,22 +37,6 @@ RSpec.describe RuboCop::Cop::RSpec::NotToNot, :config do
25
37
  it { expect(false).not_to be_true }
26
38
  RUBY
27
39
  end
28
-
29
- include_examples 'autocorrect',
30
- 'it { expect(0).to_not equal 1 }',
31
- 'it { expect(0).not_to equal 1 }'
32
-
33
- original = <<-RUBY
34
- expect {
35
- 2 + 2
36
- }.to_not raise_error
37
- RUBY
38
- corrected = <<-RUBY
39
- expect {
40
- 2 + 2
41
- }.not_to raise_error
42
- RUBY
43
- include_examples 'autocorrect', original, corrected
44
40
  end
45
41
 
46
42
  context 'when AcceptedMethod is `to_not`' do
@@ -51,6 +47,10 @@ RSpec.describe RuboCop::Cop::RSpec::NotToNot, :config do
51
47
  it { expect(false).not_to be_true }
52
48
  ^^^^^^ Prefer `to_not` over `not_to`.
53
49
  RUBY
50
+
51
+ expect_correction(<<-RUBY)
52
+ it { expect(false).to_not be_true }
53
+ RUBY
54
54
  end
55
55
 
56
56
  it 'detects the `not_to` offense on an expect block' do
@@ -60,6 +60,12 @@ RSpec.describe RuboCop::Cop::RSpec::NotToNot, :config do
60
60
  }.not_to raise_error
61
61
  ^^^^^^ Prefer `to_not` over `not_to`.
62
62
  RUBY
63
+
64
+ expect_correction(<<-RUBY)
65
+ expect {
66
+ 2 + 2
67
+ }.to_not raise_error
68
+ RUBY
63
69
  end
64
70
 
65
71
  it 'detects no offense when using `to_not`' do
@@ -67,21 +73,5 @@ RSpec.describe RuboCop::Cop::RSpec::NotToNot, :config do
67
73
  it { expect(false).to_not be_true }
68
74
  RUBY
69
75
  end
70
-
71
- include_examples 'autocorrect',
72
- 'it { expect(0).not_to equal 1 }',
73
- 'it { expect(0).to_not equal 1 }'
74
-
75
- original = <<-RUBY
76
- expect {
77
- 2 + 2
78
- }.not_to raise_error
79
- RUBY
80
- corrected = <<-RUBY
81
- expect {
82
- 2 + 2
83
- }.to_not raise_error
84
- RUBY
85
- include_examples 'autocorrect', original, corrected
86
76
  end
87
77
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  RSpec.describe RuboCop::Cop::RSpec::OverwritingSetup do
2
4
  subject(:cop) { described_class.new }
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  RSpec.describe RuboCop::Cop::RSpec::Pending do
2
4
  subject(:cop) { described_class.new }
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  RSpec.describe RuboCop::Cop::RSpec::PredicateMatcher, :config do
2
4
  subject(:cop) { described_class.new(config) }
3
5
 
@@ -8,6 +8,10 @@ RSpec.describe RuboCop::Cop::RSpec::ReceiveCounts do
8
8
  expect(foo).to receive(:bar).exactly(1).times
9
9
  ^^^^^^^^^^^^^^^^^ Use `.once` instead of `.exactly(1).times`.
10
10
  RUBY
11
+
12
+ expect_correction(<<-RUBY)
13
+ expect(foo).to receive(:bar).once
14
+ RUBY
11
15
  end
12
16
 
13
17
  it 'flags usage of `exactly(2).times`' do
@@ -15,6 +19,10 @@ RSpec.describe RuboCop::Cop::RSpec::ReceiveCounts do
15
19
  expect(foo).to receive(:bar).exactly(2).times
16
20
  ^^^^^^^^^^^^^^^^^ Use `.twice` instead of `.exactly(2).times`.
17
21
  RUBY
22
+
23
+ expect_correction(<<-RUBY)
24
+ expect(foo).to receive(:bar).twice
25
+ RUBY
18
26
  end
19
27
 
20
28
  it 'allows `exactly(3).times`' do
@@ -34,6 +42,10 @@ RSpec.describe RuboCop::Cop::RSpec::ReceiveCounts do
34
42
  expect(foo).to receive(:bar).with(baz).exactly(1).times
35
43
  ^^^^^^^^^^^^^^^^^ Use `.once` instead of `.exactly(1).times`.
36
44
  RUBY
45
+
46
+ expect_correction(<<-RUBY)
47
+ expect(foo).to receive(:bar).with(baz).once
48
+ RUBY
37
49
  end
38
50
 
39
51
  it 'flags usage of `exactly(1).times` with return value' do
@@ -41,6 +53,10 @@ RSpec.describe RuboCop::Cop::RSpec::ReceiveCounts do
41
53
  expect(foo).to receive(:bar).exactly(1).times.and_return(true)
42
54
  ^^^^^^^^^^^^^^^^^ Use `.once` instead of `.exactly(1).times`.
43
55
  RUBY
56
+
57
+ expect_correction(<<-RUBY)
58
+ expect(foo).to receive(:bar).once.and_return(true)
59
+ RUBY
44
60
  end
45
61
 
46
62
  it 'flags usage of `exactly(1).times` with a block' do
@@ -48,6 +64,10 @@ RSpec.describe RuboCop::Cop::RSpec::ReceiveCounts do
48
64
  expect(foo).to receive(:bar).exactly(1).times { true }
49
65
  ^^^^^^^^^^^^^^^^^ Use `.once` instead of `.exactly(1).times`.
50
66
  RUBY
67
+
68
+ expect_correction(<<-RUBY)
69
+ expect(foo).to receive(:bar).once { true }
70
+ RUBY
51
71
  end
52
72
 
53
73
  it 'flags usage of `at_least(1).times`' do
@@ -55,6 +75,10 @@ RSpec.describe RuboCop::Cop::RSpec::ReceiveCounts do
55
75
  expect(foo).to receive(:bar).at_least(1).times
56
76
  ^^^^^^^^^^^^^^^^^^ Use `.at_least(:once)` instead of `.at_least(1).times`.
57
77
  RUBY
78
+
79
+ expect_correction(<<-RUBY)
80
+ expect(foo).to receive(:bar).at_least(:once)
81
+ RUBY
58
82
  end
59
83
 
60
84
  it 'flags usage of `at_least(2).times`' do
@@ -62,6 +86,21 @@ RSpec.describe RuboCop::Cop::RSpec::ReceiveCounts do
62
86
  expect(foo).to receive(:bar).at_least(2).times
63
87
  ^^^^^^^^^^^^^^^^^^ Use `.at_least(:twice)` instead of `.at_least(2).times`.
64
88
  RUBY
89
+
90
+ expect_correction(<<-RUBY)
91
+ expect(foo).to receive(:bar).at_least(:twice)
92
+ RUBY
93
+ end
94
+
95
+ it 'flags usage of `at_least(2).times` with a block' do
96
+ expect_offense(<<-RUBY)
97
+ expect(foo).to receive(:bar).at_least(2).times { true }
98
+ ^^^^^^^^^^^^^^^^^^ Use `.at_least(:twice)` instead of `.at_least(2).times`.
99
+ RUBY
100
+
101
+ expect_correction(<<-RUBY)
102
+ expect(foo).to receive(:bar).at_least(:twice) { true }
103
+ RUBY
65
104
  end
66
105
 
67
106
  it 'flags usage of `at_most(1).times`' do
@@ -69,6 +108,10 @@ RSpec.describe RuboCop::Cop::RSpec::ReceiveCounts do
69
108
  expect(foo).to receive(:bar).at_most(1).times
70
109
  ^^^^^^^^^^^^^^^^^ Use `.at_most(:once)` instead of `.at_most(1).times`.
71
110
  RUBY
111
+
112
+ expect_correction(<<-RUBY)
113
+ expect(foo).to receive(:bar).at_most(:once)
114
+ RUBY
72
115
  end
73
116
 
74
117
  it 'flags usage of `at_most(2).times`' do
@@ -76,6 +119,10 @@ RSpec.describe RuboCop::Cop::RSpec::ReceiveCounts do
76
119
  expect(foo).to receive(:bar).at_most(2).times
77
120
  ^^^^^^^^^^^^^^^^^ Use `.at_most(:twice)` instead of `.at_most(2).times`.
78
121
  RUBY
122
+
123
+ expect_correction(<<-RUBY)
124
+ expect(foo).to receive(:bar).at_most(:twice)
125
+ RUBY
79
126
  end
80
127
 
81
128
  it 'allows exactly(1).times when not called on `receive`' do
@@ -84,14 +131,6 @@ RSpec.describe RuboCop::Cop::RSpec::ReceiveCounts do
84
131
  RUBY
85
132
  end
86
133
 
87
- include_examples 'autocorrect',
88
- 'expect(foo).to receive(:bar).exactly(1).times { true }',
89
- 'expect(foo).to receive(:bar).once { true }'
90
-
91
- include_examples 'autocorrect',
92
- 'expect(foo).to receive(:bar).at_least(2).times { true }',
93
- 'expect(foo).to receive(:bar).at_least(:twice) { true }'
94
-
95
134
  # Does not auto-correct if not part of the RSpec API
96
135
  include_examples 'autocorrect',
97
136
  'expect(foo).to have_published_event(:bar).exactly(2).times',
@@ -8,6 +8,10 @@ RSpec.describe RuboCop::Cop::RSpec::ReceiveNever do
8
8
  expect(foo).to receive(:bar).never
9
9
  ^^^^^ Use `not_to receive` instead of `never`.
10
10
  RUBY
11
+
12
+ expect_correction(<<-RUBY)
13
+ expect(foo).not_to receive(:bar)
14
+ RUBY
11
15
  end
12
16
 
13
17
  it 'flags usage of `never` after `with`' do
@@ -15,6 +19,10 @@ RSpec.describe RuboCop::Cop::RSpec::ReceiveNever do
15
19
  expect(foo).to receive(:bar).with(baz).never
16
20
  ^^^^^ Use `not_to receive` instead of `never`.
17
21
  RUBY
22
+
23
+ expect_correction(<<-RUBY)
24
+ expect(foo).not_to receive(:bar).with(baz)
25
+ RUBY
18
26
  end
19
27
 
20
28
  it 'flags usage of `never` with `is_expected`' do
@@ -22,6 +30,10 @@ RSpec.describe RuboCop::Cop::RSpec::ReceiveNever do
22
30
  is_expected.to receive(:bar).with(baz).never
23
31
  ^^^^^ Use `not_to receive` instead of `never`.
24
32
  RUBY
33
+
34
+ expect_correction(<<-RUBY)
35
+ is_expected.not_to receive(:bar).with(baz)
36
+ RUBY
25
37
  end
26
38
 
27
39
  it 'flags usage of `never` with `expect_any_instance_of`' do
@@ -29,6 +41,10 @@ RSpec.describe RuboCop::Cop::RSpec::ReceiveNever do
29
41
  expect_any_instance_of(Foo).to receive(:bar).with(baz).never
30
42
  ^^^^^ Use `not_to receive` instead of `never`.
31
43
  RUBY
44
+
45
+ expect_correction(<<-RUBY)
46
+ expect_any_instance_of(Foo).not_to receive(:bar).with(baz)
47
+ RUBY
32
48
  end
33
49
 
34
50
  it 'allows method called `never`' do
@@ -38,8 +54,4 @@ RSpec.describe RuboCop::Cop::RSpec::ReceiveNever do
38
54
  is_expected.to be never
39
55
  RUBY
40
56
  end
41
-
42
- include_examples 'autocorrect',
43
- 'expect(foo).to receive(:bar).with(0).never',
44
- 'expect(foo).not_to receive(:bar).with(0)'
45
57
  end