/
feature_methods_spec.rb
126 lines (110 loc) · 3.13 KB
/
feature_methods_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
116
117
118
119
120
121
122
123
124
125
126
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::RSpec::Capybara::FeatureMethods do
it 'flags violations for `background`' do
expect_offense(<<-RUBY)
describe 'some feature' do
background do; end
^^^^^^^^^^ Use `before` instead of `background`.
end
RUBY
end
it 'flags violations for `scenario`' do
expect_offense(<<-RUBY)
RSpec.describe 'some feature' do
scenario 'Foo' do; end
^^^^^^^^ Use `it` instead of `scenario`.
end
RUBY
end
it 'flags violations for `xscenario`' do
expect_offense(<<-RUBY)
describe 'Foo' do
RSpec.xscenario 'Baz' do; end
^^^^^^^^^ Use `xit` instead of `xscenario`.
end
RUBY
end
it 'flags violations for `given`' do
expect_offense(<<-RUBY)
RSpec.describe 'Foo' do
given(:foo) { :foo }
^^^^^ Use `let` instead of `given`.
end
RUBY
end
it 'flags violations for `given!`' do
expect_offense(<<-RUBY)
describe 'Foo' do
given!(:foo) { :foo }
^^^^^^ Use `let!` instead of `given!`.
end
RUBY
end
it 'flags violations for `feature`' do
expect_offense(<<-RUBY)
RSpec.feature 'Foo' do; end
^^^^^^^ Use `describe` instead of `feature`.
RUBY
end
it 'ignores variables inside examples' do
expect_no_offenses(<<-RUBY)
it 'is valid code' do
given(feature)
assign(background)
run scenario
end
RUBY
end
it 'ignores feature calls outside spec' do
expect_no_offenses(<<-RUBY)
FactoryBot.define do
factory :company do
feature { "a company" }
background { Faker::Lorem.sentence }
end
end
RUBY
end
it 'allows includes before the spec' do
expect_offense(<<-RUBY)
require 'rails_helper'
RSpec.feature 'Foo' do; end
^^^^^^^ Use `describe` instead of `feature`.
RUBY
end
context 'with configured `EnabledMethods`' do
let(:cop_config) { { 'EnabledMethods' => %w[feature] } }
it 'ignores usage of the enabled method' do
expect_no_offenses(<<-RUBY)
RSpec.feature 'feature is enabled' do; end
RUBY
end
it 'flags other methods' do
expect_offense(<<-RUBY)
RSpec.feature 'feature is enabled' do
given(:foo) { :foo }
^^^^^ Use `let` instead of `given`.
end
RUBY
end
end
shared_examples 'autocorrect_spec' do |original, corrected|
original = <<-RUBY
describe Foo do
#{original}
end
RUBY
corrected = <<-RUBY
describe Foo do
#{corrected}
end
RUBY
include_examples 'autocorrect', original, corrected
end
include_examples 'autocorrect_spec', 'background { }', 'before { }'
include_examples 'autocorrect_spec', 'scenario { }', 'it { }'
include_examples 'autocorrect_spec', 'xscenario { }', 'xit { }'
include_examples 'autocorrect_spec', 'given(:foo) { }', 'let(:foo) { }'
include_examples 'autocorrect_spec', 'given!(:foo) { }', 'let!(:foo) { }'
include_examples 'autocorrect_spec', 'RSpec.feature { }', 'RSpec.describe { }'
end