forked from rubocop/rubocop-rspec
/
let_setup_spec.rb
115 lines (98 loc) · 2.66 KB
/
let_setup_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::RSpec::LetSetup do
subject(:cop) { described_class.new }
it 'complains when let! is used and not referenced' do
expect_offense(<<-RUBY)
describe Foo do
let!(:foo) { bar }
^^^^^^^^^^ Do not use `let!` to setup objects not referenced in tests.
it 'does not use foo' do
expect(baz).to eq(qux)
end
end
RUBY
end
it 'ignores let! when used in `before`' do
expect_no_offenses(<<-RUBY)
describe Foo do
let!(:foo) { bar }
before do
foo
end
it 'does not use foo' do
expect(baz).to eq(qux)
end
end
RUBY
end
it 'ignores let! when used in example' do
expect_no_offenses(<<-RUBY)
describe Foo do
let!(:foo) { bar }
it 'uses foo' do
foo
expect(baz).to eq(qux)
end
end
RUBY
end
it 'complains when let! is used and not referenced within nested group' do
expect_offense(<<-RUBY)
describe Foo do
context 'when something special happens' do
let!(:foo) { bar }
^^^^^^^^^^ Do not use `let!` to setup objects not referenced in tests.
it 'does not use foo' do
expect(baz).to eq(qux)
end
end
it 'references some other foo' do
foo
end
end
RUBY
end
it 'complains when let! is used and not referenced in shared example group' do
expect_offense(<<-RUBY)
shared_context 'foo' do
let!(:bar) { baz }
^^^^^^^^^^ Do not use `let!` to setup objects not referenced in tests.
it 'does not use bar' do
expect(baz).to eq(qux)
end
end
RUBY
end
it 'complains when let! used in shared example including' do
expect_offense(<<-RUBY)
describe Foo do
it_behaves_like 'bar' do
let!(:baz) { foobar }
^^^^^^^^^^ Do not use `let!` to setup objects not referenced in tests.
let(:a) { b }
end
end
RUBY
end
it 'complains when there is only one nested node into example group' do
expect_offense(<<-RUBY)
describe Foo do
let!(:bar) { baz }
^^^^^^^^^^ Do not use `let!` to setup objects not referenced in tests.
end
RUBY
end
it 'complains when there is a custom nesting level' do
expect_offense(<<-RUBY)
describe Foo do
[].each do |i|
let!(:bar) { i }
^^^^^^^^^^ Do not use `let!` to setup objects not referenced in tests.
it 'does not use bar' do
expect(baz).to eq(qux)
end
end
end
RUBY
end
end