Skip to content

Commit cbd4d02

Browse files
authored
Merge pull request #2188 from sinsoku/fix_duplicate_configuration_for_driver
Fix an issue where driven_by(:selenium) is always called
2 parents 01f4269 + 850f450 commit cbd4d02

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

lib/rspec/rails/example/system_example_group.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,16 +85,19 @@ def app
8585
def initialize(*args, &blk)
8686
super(*args, &blk)
8787
@driver = nil
88+
89+
self.class.before do
90+
# A user may have already set the driver, so only default if driver
91+
# is not set
92+
driven_by(:selenium) unless @driver
93+
end
8894
end
8995

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

94100
before do
95-
# A user may have already set the driver, so only default if driver
96-
# is not set
97-
driven_by(:selenium) unless @driver
98101
@routes = ::Rails.application.routes
99102
end
100103

spec/rspec/rails/example/system_example_group_spec.rb

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,47 @@ module RSpec::Rails
1818
end
1919
end
2020
end
21+
22+
describe '#driver' do
23+
it 'uses :selenium driver by default' do
24+
group = RSpec::Core::ExampleGroup.describe do
25+
include SystemExampleGroup
26+
end
27+
example = group.new
28+
group.hooks.run(:before, :example, example)
29+
30+
expect(Capybara.current_driver).to eq :selenium
31+
end
32+
33+
it 'sets :rack_test driver using by before_action' do
34+
group = RSpec::Core::ExampleGroup.describe do
35+
include SystemExampleGroup
36+
37+
before do
38+
driven_by(:rack_test)
39+
end
40+
end
41+
example = group.new
42+
group.hooks.run(:before, :example, example)
43+
44+
expect(Capybara.current_driver).to eq :rack_test
45+
end
46+
47+
it 'calls :driven_by method only once' do
48+
group = RSpec::Core::ExampleGroup.describe do
49+
include SystemExampleGroup
50+
51+
before do
52+
driven_by(:rack_test)
53+
end
54+
end
55+
example = group.new
56+
allow(example).to receive(:driven_by).and_call_original
57+
group.hooks.run(:before, :example, example)
58+
59+
expect(example).to have_received(:driven_by).once
60+
end
61+
end
2162
end
2263
end
2364
end

0 commit comments

Comments
 (0)