Skip to content

Commit

Permalink
[WIP] Add cop for Capybara expectations set on current_path
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Rogers committed Sep 26, 2017
1 parent 3e6c427 commit 08c92e7
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/rubocop-rspec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
require 'rubocop/cop/rspec/around_block'
require 'rubocop/cop/rspec/be_eql'
require 'rubocop/cop/rspec/before_after_all'
require 'rubocop/cop/rspec/capybara/current_path_expectation'
require 'rubocop/cop/rspec/capybara/feature_methods'
require 'rubocop/cop/rspec/describe_class'
require 'rubocop/cop/rspec/describe_method'
Expand Down
30 changes: 30 additions & 0 deletions lib/rubocop/cop/rspec/capybara/current_path_expectation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module RuboCop
module Cop
module RSpec
module Capybara
# Checks for expectations set on `current_path`, where instead the
# `have_current_path` matcher (http://www.rubydoc.info/github/
# teamcapybara/capybara/master/Capybara/RSpecMatchers#have_current_path-
# instance_method) should be used on `page`, since it uses Capybara's
# waiting functionality (https://github.com/teamcapybara/capybara/blob/
# master/README.md#asynchronous-javascript-ajax-and-friends) which
# ensures that preceding actions (like `click_link`) have completed.
class CurrentPathExpectation < Cop
MSG = 'Do not set an RSpec expectation on `current_path` in ' \
'Capybara feature specs - instead, use the ' \
'`have_current_path` matcher on `page`'

def_node_matcher :expectation_set_on_current_path, <<-PATTERN
(send nil :expect :current_path)
PATTERN

def on_send(node)
expectation_set_on_current_path(node) do |match|
add_offense(node, :selector)
end
end
end
end
end
end
end
16 changes: 16 additions & 0 deletions spec/rubocop/cop/rspec/capybara/current_path_expectation_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
RSpec.describe RuboCop::Cop::RSpec::Capybara::CurrentPathExpectation do
subject(:cop) { described_class.new }

it 'flags violations for `expect(current_path)`' do
expect_offense(<<-RUBY)
expect(current_path).to eq("/callback")
^^^^^^ Do not set an RSpec expectation on `current_path` in Capybara feature specs - instead, use the `have_current_path` matcher on `page`
RUBY
end

it 'doesn\'t flag a violation for other expectations' do
expect_no_offenses(<<-RUBY)
expect(current_user).to eq(user)
RUBY
end
end

0 comments on commit 08c92e7

Please sign in to comment.