Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cb23a0f

Browse files
authored
Merge pull request rubocop#609 from rubocop-rspec/fix-feature-methods
Fix Capybara/FeatureMethods not working when there is require before the spec
2 parents e8d9f56 + 86e9e17 commit cb23a0f

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Master (Unreleased)
44

55
* Fix false positive in `RSpec/EmptyExampleGroup` cop when methods named like a RSpec method are used. ([@Darhazer][])
6+
* Fix `Capybara/FeatureMethods` not working when there is require before the spec. ([@Darhazer][])
67

78
## 1.25.1 (2018-04-10)
89

lib/rubocop/cop/rspec/capybara/feature_methods.rb

+14-3
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class FeatureMethods < Cop
6666
PATTERN
6767

6868
def on_block(node)
69-
return unless spec?(root_node)
69+
return unless inside_spec?(node)
7070

7171
feature_method(node) do |send_node, match|
7272
next if enabled?(match)
@@ -87,8 +87,19 @@ def autocorrect(node)
8787

8888
private
8989

90-
def root_node
91-
processed_source.ast
90+
def inside_spec?(node)
91+
return spec?(node) if root_node?(node)
92+
93+
root = node.ancestors.find { |parent| root_node?(parent) }
94+
spec?(root)
95+
end
96+
97+
def root_node?(node)
98+
node.parent.nil? || root_with_siblings?(node.parent)
99+
end
100+
101+
def root_with_siblings?(node)
102+
node.begin_type? && node.parent.nil?
92103
end
93104

94105
def enabled?(method_name)

spec/rubocop/cop/rspec/capybara/feature_methods_spec.rb

+9
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,15 @@
7676
RUBY
7777
end
7878

79+
it 'allows includes before the spec' do
80+
expect_offense(<<-RUBY)
81+
require 'rails_helper'
82+
83+
RSpec.feature 'Foo' do; end
84+
^^^^^^^ Use `describe` instead of `feature`.
85+
RUBY
86+
end
87+
7988
context 'with configured `EnabledMethods`' do
8089
let(:cop_config) { { 'EnabledMethods' => %w[feature] } }
8190

0 commit comments

Comments
 (0)