rubocop-rspec 1.35.0 → 1.36.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +12 -0
- data/config/default.yml +11 -0
- data/lib/rubocop/cop/rspec/capybara/current_path_expectation.rb +16 -0
- data/lib/rubocop/cop/rspec/context_method.rb +53 -0
- data/lib/rubocop/cop/rspec/describe_class.rb +7 -10
- data/lib/rubocop/cop/rspec/described_class.rb +29 -1
- data/lib/rubocop/cop/rspec/empty_line_after_example.rb +87 -0
- data/lib/rubocop/cop/rspec/example_wording.rb +28 -12
- data/lib/rubocop/cop/rspec/expect_change.rb +8 -8
- data/lib/rubocop/cop/rspec/factory_bot/attribute_defined_statically.rb +2 -9
- data/lib/rubocop/cop/rspec/focus.rb +5 -10
- data/lib/rubocop/cop/rspec/instance_variable.rb +2 -1
- data/lib/rubocop/cop/rspec/multiple_expectations.rb +13 -21
- data/lib/rubocop/cop/rspec/pending.rb +19 -25
- data/lib/rubocop/cop/rspec_cops.rb +2 -0
- data/lib/rubocop/rspec/version.rb +1 -1
- metadata +7 -198
- data/Gemfile +0 -11
- data/Rakefile +0 -87
- data/rubocop-rspec.gemspec +0 -49
- data/spec/project/changelog_spec.rb +0 -76
- data/spec/project/default_config_spec.rb +0 -76
- data/spec/project/project_requires_spec.rb +0 -20
- data/spec/rubocop/cop/rspec/align_left_let_brace_spec.rb +0 -51
- data/spec/rubocop/cop/rspec/align_right_let_brace_spec.rb +0 -51
- data/spec/rubocop/cop/rspec/any_instance_spec.rb +0 -32
- data/spec/rubocop/cop/rspec/around_block_spec.rb +0 -121
- data/spec/rubocop/cop/rspec/be_eql_spec.rb +0 -81
- data/spec/rubocop/cop/rspec/be_spec.rb +0 -35
- data/spec/rubocop/cop/rspec/before_after_all_spec.rb +0 -52
- data/spec/rubocop/cop/rspec/capybara/current_path_expectation_spec.rb +0 -65
- data/spec/rubocop/cop/rspec/capybara/feature_methods_spec.rb +0 -130
- data/spec/rubocop/cop/rspec/context_wording_spec.rb +0 -94
- data/spec/rubocop/cop/rspec/cop_spec.rb +0 -95
- data/spec/rubocop/cop/rspec/describe_class_spec.rb +0 -168
- data/spec/rubocop/cop/rspec/describe_method_spec.rb +0 -34
- data/spec/rubocop/cop/rspec/describe_symbol_spec.rb +0 -44
- data/spec/rubocop/cop/rspec/described_class_spec.rb +0 -352
- data/spec/rubocop/cop/rspec/dialect_spec.rb +0 -78
- data/spec/rubocop/cop/rspec/empty_example_group_spec.rb +0 -93
- data/spec/rubocop/cop/rspec/empty_line_after_example_group_spec.rb +0 -121
- data/spec/rubocop/cop/rspec/empty_line_after_final_let_spec.rb +0 -239
- data/spec/rubocop/cop/rspec/empty_line_after_hook_spec.rb +0 -133
- data/spec/rubocop/cop/rspec/empty_line_after_subject_spec.rb +0 -94
- data/spec/rubocop/cop/rspec/example_length_spec.rb +0 -78
- data/spec/rubocop/cop/rspec/example_without_description_spec.rb +0 -92
- data/spec/rubocop/cop/rspec/example_wording_spec.rb +0 -173
- data/spec/rubocop/cop/rspec/expect_actual_spec.rb +0 -150
- data/spec/rubocop/cop/rspec/expect_change_spec.rb +0 -86
- data/spec/rubocop/cop/rspec/expect_in_hook_spec.rb +0 -79
- data/spec/rubocop/cop/rspec/expect_output_spec.rb +0 -62
- data/spec/rubocop/cop/rspec/factory_bot/attribute_defined_statically_spec.rb +0 -208
- data/spec/rubocop/cop/rspec/factory_bot/create_list_spec.rb +0 -171
- data/spec/rubocop/cop/rspec/file_path_spec.rb +0 -200
- data/spec/rubocop/cop/rspec/focus_spec.rb +0 -139
- data/spec/rubocop/cop/rspec/hook_argument_spec.rb +0 -173
- data/spec/rubocop/cop/rspec/hooks_before_examples_spec.rb +0 -150
- data/spec/rubocop/cop/rspec/implicit_block_expectation_spec.rb +0 -135
- data/spec/rubocop/cop/rspec/implicit_expect_spec.rb +0 -102
- data/spec/rubocop/cop/rspec/implicit_subject_spec.rb +0 -188
- data/spec/rubocop/cop/rspec/instance_spy_spec.rb +0 -61
- data/spec/rubocop/cop/rspec/instance_variable_spec.rb +0 -95
- data/spec/rubocop/cop/rspec/invalid_predicate_matcher_spec.rb +0 -39
- data/spec/rubocop/cop/rspec/it_behaves_like_spec.rb +0 -47
- data/spec/rubocop/cop/rspec/iterated_expectation_spec.rb +0 -82
- data/spec/rubocop/cop/rspec/leading_subject_spec.rb +0 -119
- data/spec/rubocop/cop/rspec/leaky_constant_declaration_spec.rb +0 -91
- data/spec/rubocop/cop/rspec/let_before_examples_spec.rb +0 -137
- data/spec/rubocop/cop/rspec/let_setup_spec.rb +0 -66
- data/spec/rubocop/cop/rspec/message_chain_spec.rb +0 -23
- data/spec/rubocop/cop/rspec/message_expectation_spec.rb +0 -45
- data/spec/rubocop/cop/rspec/message_spies_spec.rb +0 -151
- data/spec/rubocop/cop/rspec/missing_example_group_argument_spec.rb +0 -57
- data/spec/rubocop/cop/rspec/multiple_describes_spec.rb +0 -30
- data/spec/rubocop/cop/rspec/multiple_expectations_spec.rb +0 -261
- data/spec/rubocop/cop/rspec/multiple_subjects_spec.rb +0 -83
- data/spec/rubocop/cop/rspec/named_subject_spec.rb +0 -123
- data/spec/rubocop/cop/rspec/nested_groups_spec.rb +0 -81
- data/spec/rubocop/cop/rspec/not_to_not_spec.rb +0 -77
- data/spec/rubocop/cop/rspec/overwriting_setup_spec.rb +0 -91
- data/spec/rubocop/cop/rspec/pending_spec.rb +0 -186
- data/spec/rubocop/cop/rspec/predicate_matcher_spec.rb +0 -354
- data/spec/rubocop/cop/rspec/rails/http_status_spec.rb +0 -90
- data/spec/rubocop/cop/rspec/receive_counts_spec.rb +0 -138
- data/spec/rubocop/cop/rspec/receive_never_spec.rb +0 -57
- data/spec/rubocop/cop/rspec/repeated_description_spec.rb +0 -76
- data/spec/rubocop/cop/rspec/repeated_example_spec.rb +0 -74
- data/spec/rubocop/cop/rspec/return_from_stub_spec.rb +0 -289
- data/spec/rubocop/cop/rspec/scattered_let_spec.rb +0 -28
- data/spec/rubocop/cop/rspec/scattered_setup_spec.rb +0 -98
- data/spec/rubocop/cop/rspec/shared_context_spec.rb +0 -137
- data/spec/rubocop/cop/rspec/shared_examples_spec.rb +0 -75
- data/spec/rubocop/cop/rspec/single_argument_message_chain_spec.rb +0 -178
- data/spec/rubocop/cop/rspec/subject_stub_spec.rb +0 -293
- data/spec/rubocop/cop/rspec/unspecified_exception_spec.rb +0 -177
- data/spec/rubocop/cop/rspec/verified_doubles_spec.rb +0 -86
- data/spec/rubocop/cop/rspec/void_expect_spec.rb +0 -49
- data/spec/rubocop/cop/rspec/yield_spec.rb +0 -81
- data/spec/rubocop/rspec/config_formatter_spec.rb +0 -52
- data/spec/rubocop/rspec/description_extractor_spec.rb +0 -63
- data/spec/rubocop/rspec/example_group_spec.rb +0 -44
- data/spec/rubocop/rspec/example_spec.rb +0 -62
- data/spec/rubocop/rspec/hook_spec.rb +0 -53
- data/spec/rubocop/rspec/language/selector_set_spec.rb +0 -53
- data/spec/rubocop/rspec/util/one_spec.rb +0 -23
- data/spec/rubocop/rspec/wording_spec.rb +0 -52
- data/spec/shared/autocorrect_behavior.rb +0 -9
- data/spec/shared/detects_style_behavior.rb +0 -9
- data/spec/shared/smoke_test_examples.rb +0 -27
- data/spec/smoke_tests/empty_spec.rb +0 -0
- data/spec/smoke_tests/factory_bot_spec.rb +0 -11
- data/spec/smoke_tests/no_tests_spec.rb +0 -4
- data/spec/smoke_tests/weird_rspec_spec.rb +0 -233
- data/spec/spec_helper.rb +0 -42
- data/spec/support/expect_offense.rb +0 -19
@@ -1,150 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
RSpec.describe RuboCop::Cop::RSpec::ExpectActual, :config do
|
4
|
-
subject(:cop) { described_class.new(config) }
|
5
|
-
|
6
|
-
it 'flags numeric literal values within expect(...)' do
|
7
|
-
expect_offense(<<-RUBY)
|
8
|
-
describe Foo do
|
9
|
-
it 'uses expect incorrectly' do
|
10
|
-
expect(123).to eq(bar)
|
11
|
-
^^^ Provide the actual you are testing to `expect(...)`.
|
12
|
-
expect(12.3).to eq(bar)
|
13
|
-
^^^^ Provide the actual you are testing to `expect(...)`.
|
14
|
-
expect(1i).to eq(bar)
|
15
|
-
^^ Provide the actual you are testing to `expect(...)`.
|
16
|
-
expect(1r).to eq(bar)
|
17
|
-
^^ Provide the actual you are testing to `expect(...)`.
|
18
|
-
end
|
19
|
-
end
|
20
|
-
RUBY
|
21
|
-
end
|
22
|
-
|
23
|
-
it 'flags boolean literal values within expect(...)' do
|
24
|
-
expect_offense(<<-RUBY)
|
25
|
-
describe Foo do
|
26
|
-
it 'uses expect incorrectly' do
|
27
|
-
expect(true).to eq(bar)
|
28
|
-
^^^^ Provide the actual you are testing to `expect(...)`.
|
29
|
-
expect(false).to eq(bar)
|
30
|
-
^^^^^ Provide the actual you are testing to `expect(...)`.
|
31
|
-
end
|
32
|
-
end
|
33
|
-
RUBY
|
34
|
-
end
|
35
|
-
|
36
|
-
it 'flags string and symbol literal values within expect(...)' do
|
37
|
-
expect_offense(<<-RUBY)
|
38
|
-
describe Foo do
|
39
|
-
it 'uses expect incorrectly' do
|
40
|
-
expect("foo").to eq(bar)
|
41
|
-
^^^^^ Provide the actual you are testing to `expect(...)`.
|
42
|
-
expect(:foo).to eq(bar)
|
43
|
-
^^^^ Provide the actual you are testing to `expect(...)`.
|
44
|
-
end
|
45
|
-
end
|
46
|
-
RUBY
|
47
|
-
end
|
48
|
-
|
49
|
-
it 'flags literal nil value within expect(...)' do
|
50
|
-
expect_offense(<<-RUBY)
|
51
|
-
describe Foo do
|
52
|
-
it 'uses expect incorrectly' do
|
53
|
-
expect(nil).to eq(bar)
|
54
|
-
^^^ Provide the actual you are testing to `expect(...)`.
|
55
|
-
end
|
56
|
-
end
|
57
|
-
RUBY
|
58
|
-
end
|
59
|
-
|
60
|
-
it 'does not flag dynamic values within expect(...)' do
|
61
|
-
expect_no_offenses(<<-'RUBY')
|
62
|
-
describe Foo do
|
63
|
-
it 'uses expect correctly' do
|
64
|
-
expect(foo).to eq(bar)
|
65
|
-
expect("foo#{baz}").to eq(bar)
|
66
|
-
expect(:"foo#{baz}").to eq(bar)
|
67
|
-
end
|
68
|
-
end
|
69
|
-
RUBY
|
70
|
-
end
|
71
|
-
|
72
|
-
it 'flags arrays containing only literal values within expect(...)' do
|
73
|
-
expect_offense(<<-RUBY)
|
74
|
-
describe Foo do
|
75
|
-
it 'uses expect incorrectly' do
|
76
|
-
expect([123]).to eq(bar)
|
77
|
-
^^^^^ Provide the actual you are testing to `expect(...)`.
|
78
|
-
expect([[123]]).to eq(bar)
|
79
|
-
^^^^^^^ Provide the actual you are testing to `expect(...)`.
|
80
|
-
end
|
81
|
-
end
|
82
|
-
RUBY
|
83
|
-
end
|
84
|
-
|
85
|
-
it 'flags hashes containing only literal values within expect(...)' do
|
86
|
-
expect_offense(<<-RUBY)
|
87
|
-
describe Foo do
|
88
|
-
it 'uses expect incorrectly' do
|
89
|
-
expect(foo: 1, bar: 2).to eq(bar)
|
90
|
-
^^^^^^^^^^^^^^ Provide the actual you are testing to `expect(...)`.
|
91
|
-
expect(foo: 1, bar: [{}]).to eq(bar)
|
92
|
-
^^^^^^^^^^^^^^^^^ Provide the actual you are testing to `expect(...)`.
|
93
|
-
end
|
94
|
-
end
|
95
|
-
RUBY
|
96
|
-
end
|
97
|
-
|
98
|
-
it 'flags ranges containing only literal values within expect(...)' do
|
99
|
-
expect_offense(<<-RUBY)
|
100
|
-
describe Foo do
|
101
|
-
it 'uses expect incorrectly' do
|
102
|
-
expect(1..2).to eq(bar)
|
103
|
-
^^^^ Provide the actual you are testing to `expect(...)`.
|
104
|
-
expect(1...2).to eq(bar)
|
105
|
-
^^^^^ Provide the actual you are testing to `expect(...)`.
|
106
|
-
end
|
107
|
-
end
|
108
|
-
RUBY
|
109
|
-
end
|
110
|
-
|
111
|
-
it 'flags regexps containing only literal values within expect(...)' do
|
112
|
-
expect_offense(<<-RUBY)
|
113
|
-
describe Foo do
|
114
|
-
it 'uses expect incorrectly' do
|
115
|
-
expect(/foo|bar/).to eq(bar)
|
116
|
-
^^^^^^^^^ Provide the actual you are testing to `expect(...)`.
|
117
|
-
end
|
118
|
-
end
|
119
|
-
RUBY
|
120
|
-
end
|
121
|
-
|
122
|
-
it 'does not flag complex values with dynamic parts within expect(...)' do
|
123
|
-
expect_no_offenses(<<-'RUBY')
|
124
|
-
describe Foo do
|
125
|
-
it 'uses expect incorrectly' do
|
126
|
-
expect.to eq(bar)
|
127
|
-
expect([foo]).to eq(bar)
|
128
|
-
expect([[foo]]).to eq(bar)
|
129
|
-
expect(foo: 1, bar: foo).to eq(bar)
|
130
|
-
expect(1..foo).to eq(bar)
|
131
|
-
expect(1...foo).to eq(bar)
|
132
|
-
expect(/foo|#{bar}/).to eq(bar)
|
133
|
-
end
|
134
|
-
end
|
135
|
-
RUBY
|
136
|
-
end
|
137
|
-
|
138
|
-
context 'when inspecting rspec-rails routing specs' do
|
139
|
-
let(:cop_config) { {} }
|
140
|
-
|
141
|
-
it 'ignores rspec-rails routing specs' do
|
142
|
-
inspect_source(
|
143
|
-
'expect(get: "/foo").to be_routeable',
|
144
|
-
'spec/routing/foo_spec.rb'
|
145
|
-
)
|
146
|
-
|
147
|
-
expect(cop.offenses).to be_empty
|
148
|
-
end
|
149
|
-
end
|
150
|
-
end
|
@@ -1,86 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
RSpec.describe RuboCop::Cop::RSpec::ExpectChange, :config do
|
4
|
-
subject(:cop) { described_class.new(config) }
|
5
|
-
|
6
|
-
let(:cop_config) do
|
7
|
-
{ 'EnforcedStyle' => enforced_style }
|
8
|
-
end
|
9
|
-
|
10
|
-
context 'with EnforcedStyle `method_call`' do
|
11
|
-
let(:enforced_style) { 'method_call' }
|
12
|
-
|
13
|
-
it 'finds blocks that contain simple message sending' do
|
14
|
-
expect_offense(<<-RUBY)
|
15
|
-
it do
|
16
|
-
expect(run).to change { User.count }.by(1)
|
17
|
-
^^^^^^^^^^^^^^^^^^^^^ Prefer `change(User, :count)`.
|
18
|
-
end
|
19
|
-
RUBY
|
20
|
-
|
21
|
-
expect_correction(<<-RUBY)
|
22
|
-
it do
|
23
|
-
expect(run).to change(User, :count).by(1)
|
24
|
-
end
|
25
|
-
RUBY
|
26
|
-
end
|
27
|
-
|
28
|
-
it 'ignores blocks that cannot be converted to obj/attribute pair' do
|
29
|
-
expect_no_offenses(<<-RUBY)
|
30
|
-
it do
|
31
|
-
expect(run).to change { User.sum(:points) }
|
32
|
-
end
|
33
|
-
RUBY
|
34
|
-
end
|
35
|
-
|
36
|
-
it 'ignores change method of object that happens to receive a block' do
|
37
|
-
expect_no_offenses(<<-RUBY)
|
38
|
-
it do
|
39
|
-
Record.change { User.count }
|
40
|
-
end
|
41
|
-
RUBY
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
context 'with EnforcedStyle `block`' do
|
46
|
-
let(:enforced_style) { 'block' }
|
47
|
-
|
48
|
-
it 'finds change matcher without block' do
|
49
|
-
expect_offense(<<-RUBY)
|
50
|
-
it do
|
51
|
-
expect(run).to change(User, :count).by(1)
|
52
|
-
^^^^^^^^^^^^^^^^^^^^ Prefer `change { User.count }`.
|
53
|
-
end
|
54
|
-
RUBY
|
55
|
-
|
56
|
-
expect_correction(<<-RUBY)
|
57
|
-
it do
|
58
|
-
expect(run).to change { User.count }.by(1)
|
59
|
-
end
|
60
|
-
RUBY
|
61
|
-
end
|
62
|
-
|
63
|
-
it 'finds change matcher when receiver is a variable' do
|
64
|
-
expect_offense(<<-RUBY)
|
65
|
-
it do
|
66
|
-
expect(run).to change(user, :count)
|
67
|
-
^^^^^^^^^^^^^^^^^^^^ Prefer `change { user.count }`.
|
68
|
-
end
|
69
|
-
RUBY
|
70
|
-
|
71
|
-
expect_correction(<<-RUBY)
|
72
|
-
it do
|
73
|
-
expect(run).to change { user.count }
|
74
|
-
end
|
75
|
-
RUBY
|
76
|
-
end
|
77
|
-
|
78
|
-
it 'ignores methods called change' do
|
79
|
-
expect_no_offenses(<<-RUBY)
|
80
|
-
it do
|
81
|
-
record.change(user, :count)
|
82
|
-
end
|
83
|
-
RUBY
|
84
|
-
end
|
85
|
-
end
|
86
|
-
end
|
@@ -1,79 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
RSpec.describe RuboCop::Cop::RSpec::ExpectInHook do
|
4
|
-
subject(:cop) { described_class.new }
|
5
|
-
|
6
|
-
it 'adds an offense for `expect` in `before` hook' do
|
7
|
-
expect_offense(<<-RUBY)
|
8
|
-
before do
|
9
|
-
expect(something).to eq('foo')
|
10
|
-
^^^^^^ Do not use `expect` in `before` hook
|
11
|
-
is_expected.to eq('foo')
|
12
|
-
^^^^^^^^^^^ Do not use `is_expected` in `before` hook
|
13
|
-
expect_any_instance_of(Something).to receive(:foo)
|
14
|
-
^^^^^^^^^^^^^^^^^^^^^^ Do not use `expect_any_instance_of` in `before` hook
|
15
|
-
end
|
16
|
-
RUBY
|
17
|
-
end
|
18
|
-
|
19
|
-
it 'adds an offense for `expect` in `after` hook' do
|
20
|
-
expect_offense(<<-RUBY)
|
21
|
-
after do
|
22
|
-
expect(something).to eq('foo')
|
23
|
-
^^^^^^ Do not use `expect` in `after` hook
|
24
|
-
is_expected.to eq('foo')
|
25
|
-
^^^^^^^^^^^ Do not use `is_expected` in `after` hook
|
26
|
-
expect_any_instance_of(Something).to receive(:foo)
|
27
|
-
^^^^^^^^^^^^^^^^^^^^^^ Do not use `expect_any_instance_of` in `after` hook
|
28
|
-
end
|
29
|
-
RUBY
|
30
|
-
end
|
31
|
-
|
32
|
-
it 'adds an offense for `expect` in `around` hook' do
|
33
|
-
expect_offense(<<-RUBY)
|
34
|
-
around do
|
35
|
-
expect(something).to eq('foo')
|
36
|
-
^^^^^^ Do not use `expect` in `around` hook
|
37
|
-
is_expected(something).to eq('foo')
|
38
|
-
^^^^^^^^^^^ Do not use `is_expected` in `around` hook
|
39
|
-
expect_any_instance_of(Something).to receive(:foo)
|
40
|
-
^^^^^^^^^^^^^^^^^^^^^^ Do not use `expect_any_instance_of` in `around` hook
|
41
|
-
end
|
42
|
-
RUBY
|
43
|
-
end
|
44
|
-
|
45
|
-
it 'adds an offense for `expect` with block in `before` hook' do
|
46
|
-
expect_offense(<<-RUBY)
|
47
|
-
before do
|
48
|
-
expect { something }.to eq('foo')
|
49
|
-
^^^^^^ Do not use `expect` in `before` hook
|
50
|
-
end
|
51
|
-
RUBY
|
52
|
-
end
|
53
|
-
|
54
|
-
it 'accepts an empty `before` hook' do
|
55
|
-
expect_no_offenses(<<-RUBY)
|
56
|
-
before do
|
57
|
-
end
|
58
|
-
RUBY
|
59
|
-
end
|
60
|
-
|
61
|
-
it 'accepts `allow` in `before` hook' do
|
62
|
-
expect_no_offenses(<<-RUBY)
|
63
|
-
before do
|
64
|
-
allow(something).to receive(:foo)
|
65
|
-
allow_any_instance_of(something).to receive(:foo)
|
66
|
-
end
|
67
|
-
RUBY
|
68
|
-
end
|
69
|
-
|
70
|
-
it 'accepts `expect` in `it`' do
|
71
|
-
expect_no_offenses(<<-RUBY)
|
72
|
-
it do
|
73
|
-
expect(something).to eq('foo')
|
74
|
-
is_expected.to eq('foo')
|
75
|
-
expect_any_instance_of(something).to receive(:foo)
|
76
|
-
end
|
77
|
-
RUBY
|
78
|
-
end
|
79
|
-
end
|
@@ -1,62 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'spec_helper'
|
4
|
-
|
5
|
-
RSpec.describe RuboCop::Cop::RSpec::ExpectOutput do
|
6
|
-
subject(:cop) { described_class.new }
|
7
|
-
|
8
|
-
it 'registers an offense for overwriting $stdout within an example' do
|
9
|
-
expect_offense(<<-RUBY)
|
10
|
-
specify do
|
11
|
-
$stdout = StringIO.new
|
12
|
-
^^^^^^^ Use `expect { ... }.to output(...).to_stdout` instead of mutating $stdout.
|
13
|
-
end
|
14
|
-
RUBY
|
15
|
-
end
|
16
|
-
|
17
|
-
it 'registers an offense for overwriting $stderr ' \
|
18
|
-
'within an example scoped hook' do
|
19
|
-
expect_offense(<<-RUBY)
|
20
|
-
before(:each) do
|
21
|
-
$stderr = StringIO.new
|
22
|
-
^^^^^^^ Use `expect { ... }.to output(...).to_stderr` instead of mutating $stderr.
|
23
|
-
end
|
24
|
-
RUBY
|
25
|
-
end
|
26
|
-
|
27
|
-
it 'does not register an offense for interacting with $stdout' do
|
28
|
-
expect_no_offenses(<<-RUBY)
|
29
|
-
specify do
|
30
|
-
$stdout.puts("hi")
|
31
|
-
end
|
32
|
-
RUBY
|
33
|
-
end
|
34
|
-
|
35
|
-
it 'does not flag assignments to other global variables' do
|
36
|
-
expect_no_offenses(<<-RUBY)
|
37
|
-
specify do
|
38
|
-
$blah = StringIO.new
|
39
|
-
end
|
40
|
-
RUBY
|
41
|
-
end
|
42
|
-
|
43
|
-
it 'does not flag assignments to $stdout outside of example scope' do
|
44
|
-
expect_no_offenses(<<-RUBY)
|
45
|
-
before(:suite) do
|
46
|
-
$stderr = StringIO.new
|
47
|
-
end
|
48
|
-
RUBY
|
49
|
-
end
|
50
|
-
|
51
|
-
it 'does not flag assignments to $stdout in example_group scope' do
|
52
|
-
expect_no_offenses(<<-RUBY)
|
53
|
-
describe Foo do
|
54
|
-
$stderr = StringIO.new
|
55
|
-
end
|
56
|
-
RUBY
|
57
|
-
end
|
58
|
-
|
59
|
-
it 'does not flag assigns to $stdout when in the root scope' do
|
60
|
-
expect_no_offenses('$stderr = StringIO.new')
|
61
|
-
end
|
62
|
-
end
|
@@ -1,208 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
RSpec.describe RuboCop::Cop::RSpec::FactoryBot::AttributeDefinedStatically do # rubocop:disable Metrics/LineLength
|
4
|
-
subject(:cop) { described_class.new }
|
5
|
-
|
6
|
-
it 'registers an offense for offending code' do
|
7
|
-
expect_offense(<<-RUBY)
|
8
|
-
FactoryBot.define do
|
9
|
-
factory :post do
|
10
|
-
title "Something"
|
11
|
-
^^^^^^^^^^^^^^^^^ Use a block to declare attribute values.
|
12
|
-
published_at 1.day.from_now
|
13
|
-
^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use a block to declare attribute values.
|
14
|
-
status [:draft, :published].sample
|
15
|
-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use a block to declare attribute values.
|
16
|
-
created_at 1.day.ago
|
17
|
-
^^^^^^^^^^^^^^^^^^^^ Use a block to declare attribute values.
|
18
|
-
update_times [Time.current]
|
19
|
-
^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use a block to declare attribute values.
|
20
|
-
meta_tags(foo: Time.current)
|
21
|
-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use a block to declare attribute values.
|
22
|
-
end
|
23
|
-
end
|
24
|
-
RUBY
|
25
|
-
end
|
26
|
-
|
27
|
-
it 'registers an offense in a trait' do
|
28
|
-
expect_offense(<<-RUBY)
|
29
|
-
FactoryBot.define do
|
30
|
-
factory :post do
|
31
|
-
trait :published do
|
32
|
-
title "Something"
|
33
|
-
^^^^^^^^^^^^^^^^^ Use a block to declare attribute values.
|
34
|
-
published_at 1.day.from_now
|
35
|
-
^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use a block to declare attribute values.
|
36
|
-
end
|
37
|
-
end
|
38
|
-
end
|
39
|
-
RUBY
|
40
|
-
end
|
41
|
-
|
42
|
-
it 'registers an offense in a transient block' do
|
43
|
-
expect_offense(<<-RUBY)
|
44
|
-
FactoryBot.define do
|
45
|
-
factory :post do
|
46
|
-
transient do
|
47
|
-
title "Something"
|
48
|
-
^^^^^^^^^^^^^^^^^ Use a block to declare attribute values.
|
49
|
-
published_at 1.day.from_now
|
50
|
-
^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use a block to declare attribute values.
|
51
|
-
end
|
52
|
-
end
|
53
|
-
end
|
54
|
-
RUBY
|
55
|
-
end
|
56
|
-
|
57
|
-
it 'registers an offense for an attribute defined on `self`' do
|
58
|
-
expect_offense(<<-RUBY)
|
59
|
-
FactoryBot.define do
|
60
|
-
factory :post do
|
61
|
-
self.start { Date.today }
|
62
|
-
self.end Date.tomorrow
|
63
|
-
^^^^^^^^^^^^^^^^^^^^^^ Use a block to declare attribute values.
|
64
|
-
end
|
65
|
-
end
|
66
|
-
RUBY
|
67
|
-
end
|
68
|
-
|
69
|
-
it 'registers an offense for attributes defined on explicit receiver' do
|
70
|
-
expect_offense(<<-RUBY)
|
71
|
-
FactoryBot.define do
|
72
|
-
factory :post do |post_definition|
|
73
|
-
post_definition.end Date.tomorrow
|
74
|
-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use a block to declare attribute values.
|
75
|
-
post_definition.trait :published do |published_definition|
|
76
|
-
published_definition.published_at 1.day.from_now
|
77
|
-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use a block to declare attribute values.
|
78
|
-
end
|
79
|
-
end
|
80
|
-
end
|
81
|
-
RUBY
|
82
|
-
end
|
83
|
-
|
84
|
-
it 'accepts valid factory definitions' do
|
85
|
-
expect_no_offenses(<<-RUBY)
|
86
|
-
FactoryBot.define do
|
87
|
-
factory :post do
|
88
|
-
trait :published do
|
89
|
-
published_at { 1.day.from_now }
|
90
|
-
end
|
91
|
-
created_at { 1.day.ago }
|
92
|
-
status { :draft }
|
93
|
-
comments_count { 0 }
|
94
|
-
title { "Static" }
|
95
|
-
description { FFaker::Lorem.paragraph(10) }
|
96
|
-
recent_statuses { [] }
|
97
|
-
tags { { like_count: 2 } }
|
98
|
-
|
99
|
-
before(:create, &:initialize_something)
|
100
|
-
after(:create, &:rebuild_cache)
|
101
|
-
end
|
102
|
-
end
|
103
|
-
RUBY
|
104
|
-
end
|
105
|
-
|
106
|
-
it 'does not add offense if out of factory bot block' do
|
107
|
-
expect_no_offenses(<<-RUBY)
|
108
|
-
status [:draft, :published].sample
|
109
|
-
published_at 1.day.from_now
|
110
|
-
created_at 1.day.ago
|
111
|
-
update_times [Time.current]
|
112
|
-
meta_tags(foo: Time.current)
|
113
|
-
RUBY
|
114
|
-
end
|
115
|
-
|
116
|
-
it 'does not add offense if method called on another object' do
|
117
|
-
expect_no_offenses(<<-RUBY)
|
118
|
-
FactoryBot.define do
|
119
|
-
factory :post do |post_definition|
|
120
|
-
Registrar.register :post_factory
|
121
|
-
end
|
122
|
-
end
|
123
|
-
RUBY
|
124
|
-
end
|
125
|
-
|
126
|
-
it 'does not add offense if method called on a local variable' do
|
127
|
-
expect_no_offenses(<<-RUBY)
|
128
|
-
FactoryBot.define do
|
129
|
-
factory :post do |post_definition|
|
130
|
-
local = Registrar
|
131
|
-
local.register :post_factory
|
132
|
-
end
|
133
|
-
end
|
134
|
-
RUBY
|
135
|
-
end
|
136
|
-
|
137
|
-
it 'accepts valid association definitions' do
|
138
|
-
expect_no_offenses(<<-RUBY)
|
139
|
-
FactoryBot.define do
|
140
|
-
factory :post do
|
141
|
-
author age: 42, factory: :user
|
142
|
-
end
|
143
|
-
end
|
144
|
-
RUBY
|
145
|
-
end
|
146
|
-
|
147
|
-
it 'accepts valid sequence definition' do
|
148
|
-
expect_no_offenses(<<-RUBY)
|
149
|
-
FactoryBot.define do
|
150
|
-
factory :post do
|
151
|
-
sequence :negative_numbers, &:-@
|
152
|
-
end
|
153
|
-
end
|
154
|
-
RUBY
|
155
|
-
end
|
156
|
-
|
157
|
-
bad = <<-RUBY
|
158
|
-
FactoryBot.define do
|
159
|
-
factory :post do
|
160
|
-
title "Something"
|
161
|
-
comments_count 0
|
162
|
-
tag Tag::MAGIC
|
163
|
-
recent_statuses []
|
164
|
-
status([:draft, :published].sample)
|
165
|
-
published_at 1.day.from_now
|
166
|
-
created_at(1.day.ago)
|
167
|
-
updated_at Time.current
|
168
|
-
update_times [Time.current]
|
169
|
-
meta_tags(foo: Time.current)
|
170
|
-
other_tags({ foo: Time.current })
|
171
|
-
options color: :blue
|
172
|
-
other_options Tag::MAGIC => :magic
|
173
|
-
self.end Date.tomorrow
|
174
|
-
|
175
|
-
trait :old do
|
176
|
-
published_at 1.week.ago
|
177
|
-
end
|
178
|
-
end
|
179
|
-
end
|
180
|
-
RUBY
|
181
|
-
|
182
|
-
corrected = <<-RUBY
|
183
|
-
FactoryBot.define do
|
184
|
-
factory :post do
|
185
|
-
title { "Something" }
|
186
|
-
comments_count { 0 }
|
187
|
-
tag { Tag::MAGIC }
|
188
|
-
recent_statuses { [] }
|
189
|
-
status { [:draft, :published].sample }
|
190
|
-
published_at { 1.day.from_now }
|
191
|
-
created_at { 1.day.ago }
|
192
|
-
updated_at { Time.current }
|
193
|
-
update_times { [Time.current] }
|
194
|
-
meta_tags { { foo: Time.current } }
|
195
|
-
other_tags { { foo: Time.current } }
|
196
|
-
options { { color: :blue } }
|
197
|
-
other_options { { Tag::MAGIC => :magic } }
|
198
|
-
self.end { Date.tomorrow }
|
199
|
-
|
200
|
-
trait :old do
|
201
|
-
published_at { 1.week.ago }
|
202
|
-
end
|
203
|
-
end
|
204
|
-
end
|
205
|
-
RUBY
|
206
|
-
|
207
|
-
include_examples 'autocorrect', bad, corrected
|
208
|
-
end
|