Skip to content

Commit 79dbd91

Browse files
Testing Generator: Improvements
Follow-up to #1156 Creates parity with Rails' decision to [use a headless driver by default][headless]. This will be fixed in an [upcoming release][rspec] of rspec-rails, but I felt it was important to capture here. Additionally, it ensures the `screen_size` is the same as what is set in Rails. Removes `webdrivers` dependency in favor of `selenium-webdriver`. This generator assumes the app was generated with the `--skip-test`, which means we need to add the `selenium-webdriver` and `capybara` gems. Updates `action_dispatch-testing-integration-capybara` dependency to the most recent tagged release in an effort to suppress Dependabot notifications. Ensure all files under `spec/support` are loaded by uncommenting a line generated by the RSpec installation script. [headless]: rails/rails#50512 [rspec]: rspec/rspec-rails#2746
1 parent 39efb28 commit 79dbd91

File tree

5 files changed

+22
-66
lines changed

5 files changed

+22
-66
lines changed

README.md

+2-9
Original file line numberDiff line numberDiff line change
@@ -178,17 +178,10 @@ bin/rails g suspenders:email
178178

179179
### Testing
180180

181-
Set up the project for an in-depth test-driven development workflow.
182-
183-
Installs and configures [rspec-rails][],
184-
[action_dispatch-testing-integration-capybara][], [shoulda-matchers][],
185-
[webdrivers][] and [webmock][].
181+
Set up the project for an in-depth test-driven development workflow via
182+
[rspec-rails][] and friends.
186183

187184
[rspec-rails]: https://github.com/rspec/rspec-rails
188-
[action_dispatch-testing-integration-capybara]: https://github.com/thoughtbot/action_dispatch-testing-integration-capybara
189-
[shoulda-matchers]: https://github.com/thoughtbot/shoulda-matchers
190-
[webdrivers]: https://github.com/titusfortner/webdrivers
191-
[webmock]: https://github.com/bblimke/webmock
192185

193186
## Contributing
194187

lib/generators/suspenders/testing_generator.rb

+6-3
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ def add_gems
1010
end
1111

1212
gem_group :test do
13+
gem "capybara"
1314
gem "action_dispatch-testing-integration-capybara",
14-
github: "thoughtbot/action_dispatch-testing-integration-capybara", tag: "v0.1.0",
15+
github: "thoughtbot/action_dispatch-testing-integration-capybara", tag: "v0.1.1",
1516
require: "action_dispatch/testing/integration/capybara/rspec"
17+
gem "selenium-webdriver"
1618
gem "shoulda-matchers", "~> 6.0"
17-
gem "webdrivers"
1819
gem "webmock"
1920
end
2021

@@ -29,6 +30,8 @@ def modify_rails_helper
2930
insert_into_file "spec/rails_helper.rb",
3031
"\s\sconfig.infer_base_class_for_anonymous_controllers = false\n",
3132
after: "RSpec.configure do |config|\n"
33+
34+
uncomment_lines "spec/rails_helper.rb", /Rails\.root\.glob/
3235
end
3336

3437
def modify_spec_helper
@@ -59,7 +62,7 @@ def create_system_spec_dir
5962
end
6063

6164
def configure_chromedriver
62-
copy_file "chromedriver.rb", "spec/support/chromedriver.rb"
65+
copy_file "driver.rb", "spec/support/driver.rb"
6366
end
6467

6568
def configure_i18n_helper

lib/generators/templates/testing/chromedriver.rb

-27
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
RSpec.configure do |config|
2+
config.before(:each, type: :system) do
3+
driven_by :selenium, using: :headless_chrome, screen_size: [1400, 1400]
4+
end
5+
end

test/generators/suspenders/testing_generator_test.rb

+9-27
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ class TestingGeneratorTest < Rails::Generators::TestCase
1818
end
1919
2020
group :test do
21-
gem "action_dispatch-testing-integration-capybara", github: "thoughtbot/action_dispatch-testing-integration-capybara", tag: "v0.1.0", require: "action_dispatch/testing/integration/capybara/rspec"
21+
gem "capybara"
22+
gem "action_dispatch-testing-integration-capybara", github: "thoughtbot/action_dispatch-testing-integration-capybara", tag: "v0.1.1", require: "action_dispatch/testing/integration/capybara/rspec"
23+
gem "selenium-webdriver"
2224
gem "shoulda-matchers", "~> 6.0"
23-
gem "webdrivers"
2425
gem "webmock"
2526
end
2627
RUBY
@@ -52,6 +53,7 @@ class TestingGeneratorTest < Rails::Generators::TestCase
5253
assert_file "spec/rails_helper.rb" do |file|
5354
assert_match(/RSpec\.configure do \|config\|\s{3}config\.infer_base_class_for_anonymous_controllers\s*=\s*false/m,
5455
file)
56+
assert_match(/^\#{0}\s*Rails\.root\.glob\(\"spec\/support\/\*\*\/\*\.rb\"\)\.sort\.each { \|f\| require f }/, file)
5557
end
5658
end
5759

@@ -90,40 +92,18 @@ class TestingGeneratorTest < Rails::Generators::TestCase
9092
end
9193
end
9294

93-
test "configures Chromedriver" do
95+
test "configures driver" do
9496
expected = <<~RUBY
95-
require "selenium/webdriver"
96-
97-
Capybara.register_driver :chrome do |app|
98-
Capybara::Selenium::Driver.new(app, browser: :chrome)
99-
end
100-
101-
Capybara.register_driver :headless_chrome do |app|
102-
options = ::Selenium::WebDriver::Chrome::Options.new
103-
options.headless!
104-
options.add_argument "--window-size=1680,1050"
105-
106-
Capybara::Selenium::Driver.new app,
107-
browser: :chrome,
108-
options: options
109-
end
110-
111-
Capybara.javascript_driver = :headless_chrome
112-
11397
RSpec.configure do |config|
11498
config.before(:each, type: :system) do
115-
driven_by :rack_test
116-
end
117-
118-
config.before(:each, type: :system, js: true) do
119-
driven_by Capybara.javascript_driver
99+
driven_by :selenium, using: :headless_chrome, screen_size: [1400, 1400]
120100
end
121101
end
122102
RUBY
123103

124104
run_generator
125105

126-
assert_file app_root("spec/support/chromedriver.rb") do |file|
106+
assert_file app_root("spec/support/driver.rb") do |file|
127107
assert_equal expected, file
128108
end
129109
end
@@ -210,6 +190,8 @@ def rails_helper
210190
abort("The Rails environment is running in production mode!") if Rails.env.production?
211191
require 'rspec/rails'
212192
193+
# Rails.root.glob("spec/support/**/*.rb").sort.each { |f| require f }
194+
213195
begin
214196
ActiveRecord::Migration.maintain_test_schema!
215197
rescue ActiveRecord::PendingMigrationError => e

0 commit comments

Comments
 (0)