-
-
Notifications
You must be signed in to change notification settings - Fork 278
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add cop for Capybara expectations set on
current_path
Setting expectations on `current_path` in our Capybara feature specs leads to flaky tests. Switching to using [Capybara's `have_current_path` matcher](http://www.rubydoc.info/github/teamcapybara/capybara/master/Capybara/RSpecMatchers#have_current_path-instance_method) helps since it uses Capybara's [waiting functionality](https://github.com/teamcapybara/capybara/blob/master/README.md#asynchronous-javascript-ajax-and-friends) that ensures that preceding actions (like `click_link`) have completed. This cop tells you not to use `expect(current_path).to match(/lol/)`, encouraging you to use the `have_current_path` matcher instead.
- Loading branch information
Tim Rogers
committed
Sep 26, 2017
1 parent
3e6c427
commit e4f5d41
Showing
3 changed files
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
lib/rubocop/cop/rspec/capybara/current_path_expectation.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 (send nil :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
16
spec/rubocop/cop/rspec/capybara/current_path_expectation_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |