Skip to content

Commit

Permalink
Fix an issue where driven_by(:selenium) is always called
Browse files Browse the repository at this point in the history
Because the before process defined in SystemExampleGroup is executed
before the use defined before process, driven_by(:selenium) is always
executed.

This commit fixes to check the driver configuration at the end of
before hooks.
  • Loading branch information
sinsoku committed Oct 17, 2019
1 parent 33ab374 commit 850f450
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
9 changes: 6 additions & 3 deletions lib/rspec/rails/example/system_example_group.rb
Expand Up @@ -78,16 +78,19 @@ def app
def initialize(*args, &blk)
super(*args, &blk)
@driver = nil

self.class.before do
# A user may have already set the driver, so only default if driver
# is not set
driven_by(:selenium) unless @driver
end
end

def driven_by(*args, &blk)
@driver = ::ActionDispatch::SystemTestCase.driven_by(*args, &blk).tap(&:use)
end

before do
# A user may have already set the driver, so only default if driver
# is not set
driven_by(:selenium) unless @driver
@routes = ::Rails.application.routes
end

Expand Down
41 changes: 41 additions & 0 deletions spec/rspec/rails/example/system_example_group_spec.rb
Expand Up @@ -18,6 +18,47 @@ module RSpec::Rails
end
end
end

describe '#driver' do
it 'uses :selenium driver by default' do
group = RSpec::Core::ExampleGroup.describe do
include SystemExampleGroup
end
example = group.new
group.hooks.run(:before, :example, example)

expect(Capybara.current_driver).to eq :selenium
end

it 'sets :rack_test driver using by before_action' do
group = RSpec::Core::ExampleGroup.describe do
include SystemExampleGroup

before do
driven_by(:rack_test)
end
end
example = group.new
group.hooks.run(:before, :example, example)

expect(Capybara.current_driver).to eq :rack_test
end

it 'calls :driven_by method only once' do
group = RSpec::Core::ExampleGroup.describe do
include SystemExampleGroup

before do
driven_by(:rack_test)
end
end
example = group.new
allow(example).to receive(:driven_by).and_call_original
group.hooks.run(:before, :example, example)

expect(example).to have_received(:driven_by).once
end
end
end
end
end

0 comments on commit 850f450

Please sign in to comment.