Skip to content

Commit 5a252e8

Browse files
committed
Refactor system test integration (delay loading). (#1903)
Delay loading system tests as late as possible in order to allow people who arent using them to not have to turn off puma or other such things. It also enables other webservers to be configured other than puma which is needed in Rails as of rails/rails@50f6976
1 parent 7ed9300 commit 5a252e8

File tree

1 file changed

+77
-94
lines changed

1 file changed

+77
-94
lines changed

lib/rspec/rails/example/system_example_group.rb

Lines changed: 77 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,113 +1,96 @@
1-
can_load_system_tests = false
2-
begin
3-
require 'puma'
4-
require 'capybara'
5-
if ActionPack::VERSION::STRING >= "5.1"
6-
require 'action_dispatch/system_test_case'
7-
can_load_system_tests = true
8-
end
9-
# rubocop:disable Lint/HandleExceptions
10-
rescue LoadError
11-
# rubocop:enable Lint/HandleExceptions
12-
end
1+
module RSpec
2+
module Rails
3+
# @api public
4+
# Container class for system tests
5+
module SystemExampleGroup
6+
extend ActiveSupport::Concern
7+
include RSpec::Rails::RailsExampleGroup
8+
include RSpec::Rails::Matchers::RedirectTo
9+
include RSpec::Rails::Matchers::RenderTemplate
10+
include ActionDispatch::Integration::Runner
11+
include ActionDispatch::Assertions
12+
include ActionController::TemplateAssertions
1313

14-
if !can_load_system_tests
15-
module RSpec
16-
module Rails
17-
module SystemExampleGroup
18-
extend ActiveSupport::Concern
19-
20-
included do
21-
abort """
22-
System test integration requires Rails >= 5.1 and has a hard
23-
dependency on `puma` and `capybara`, please add these to your
24-
Gemfile before attempting to use system tests.
25-
"""
14+
# @private
15+
module BlowAwayAfterTeardownHook
16+
# @private
17+
def after_teardown
2618
end
2719
end
28-
end
29-
end
30-
else
31-
module RSpec
32-
module Rails
33-
# @api public
34-
# Container class for system tests
35-
module SystemExampleGroup
36-
extend ActiveSupport::Concern
37-
include RSpec::Rails::RailsExampleGroup
38-
include ActionDispatch::Integration::Runner
39-
include ActionDispatch::Assertions
40-
include RSpec::Rails::Matchers::RedirectTo
41-
include RSpec::Rails::Matchers::RenderTemplate
42-
include ActionController::TemplateAssertions
43-
44-
include ActionDispatch::IntegrationTest::Behavior
4520

46-
# @private
47-
module BlowAwayAfterTeardownHook
48-
# @private
49-
def after_teardown
50-
end
51-
end
21+
# for the SystemTesting Screenshot situation
22+
def passed?
23+
RSpec.current_example.exception.nil?
24+
end
5225

53-
original_after_teardown = ::ActionDispatch::SystemTesting::TestHelpers::SetupAndTeardown.instance_method(:after_teardown)
26+
# @private
27+
def method_name
28+
@method_name ||= [
29+
self.class.name.underscore,
30+
RSpec.current_example.description.underscore,
31+
rand(1000)
32+
].join("_").gsub(/[\/\.:, ]/, "_")
33+
end
5434

55-
include ::ActionDispatch::SystemTesting::TestHelpers::SetupAndTeardown
56-
include ::ActionDispatch::SystemTesting::TestHelpers::ScreenshotHelper
57-
include BlowAwayAfterTeardownHook
35+
# Delegates to `Rails.application`.
36+
def app
37+
::Rails.application
38+
end
5839

59-
# for the SystemTesting Screenshot situation
60-
def passed?
61-
RSpec.current_example.exception.nil?
40+
included do |other|
41+
begin
42+
require 'capybara'
43+
require 'action_dispatch/system_test_case'
44+
# rubocop:disable Lint/HandleExceptions
45+
rescue LoadError
46+
# rubocop:enable Lint/HandleExceptions
47+
abort """
48+
System test integration requires Rails >= 5.1 and has a hard
49+
dependency on a webserver and `capybara`, please add capybara to
50+
your Gemfile and configure a webserver (e.g. `Capybara.server =
51+
:webrick`) before attempting to use system tests.
52+
""".gsub(/[\n\s]+/,' ').strip
6253
end
6354

64-
# @private
65-
def method_name
66-
@method_name ||= [
67-
self.class.name.underscore,
68-
RSpec.current_example.description.underscore,
69-
rand(1000)
70-
].join("_").gsub(/[\/\.:, ]/, "_")
71-
end
55+
original_after_teardown =
56+
::ActionDispatch::SystemTesting::TestHelpers::SetupAndTeardown.instance_method(:after_teardown)
7257

73-
# Delegates to `Rails.application`.
74-
def app
75-
::Rails.application
76-
end
58+
other.include ActionDispatch::IntegrationTest::Behavior
59+
other.include ::ActionDispatch::SystemTesting::TestHelpers::SetupAndTeardown
60+
other.include ::ActionDispatch::SystemTesting::TestHelpers::ScreenshotHelper
61+
other.include BlowAwayAfterTeardownHook
7762

78-
included do
79-
attr_reader :driver
63+
attr_reader :driver
8064

81-
if ActionDispatch::SystemTesting::Server.respond_to?(:silence_puma=)
82-
ActionDispatch::SystemTesting::Server.silence_puma = true
83-
end
65+
if ActionDispatch::SystemTesting::Server.respond_to?(:silence_puma=)
66+
ActionDispatch::SystemTesting::Server.silence_puma = true
67+
end
8468

85-
def initialize(*args, &blk)
86-
super(*args, &blk)
87-
@driver = nil
88-
end
69+
def initialize(*args, &blk)
70+
super(*args, &blk)
71+
@driver = nil
72+
end
8973

90-
def driven_by(*args, &blk)
91-
@driver = ::ActionDispatch::SystemTestCase.driven_by(*args, &blk).tap(&:use)
92-
end
74+
def driven_by(*args, &blk)
75+
@driver = ::ActionDispatch::SystemTestCase.driven_by(*args, &blk).tap(&:use)
76+
end
9377

94-
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
98-
@routes = ::Rails.application.routes
99-
end
78+
before do
79+
# A user may have already set the driver, so only default if driver
80+
# is not set
81+
driven_by(:selenium) unless @driver
82+
@routes = ::Rails.application.routes
83+
end
10084

101-
after do
102-
orig_stdout = $stdout
103-
$stdout = StringIO.new
104-
begin
105-
original_after_teardown.bind(self).call
106-
ensure
107-
myio = $stdout
108-
RSpec.current_example.metadata[:extra_failure_lines] = myio.string
109-
$stdout = orig_stdout
110-
end
85+
after do
86+
orig_stdout = $stdout
87+
$stdout = StringIO.new
88+
begin
89+
original_after_teardown.bind(self).call
90+
ensure
91+
myio = $stdout
92+
RSpec.current_example.metadata[:extra_failure_lines] = myio.string
93+
$stdout = orig_stdout
11194
end
11295
end
11396
end

0 commit comments

Comments
 (0)