Skip to content

Commit

Permalink
Testing Generator: Improvements (#1168)
Browse files Browse the repository at this point in the history
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` flag,
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
  • Loading branch information
stevepolitodesign committed Mar 21, 2024
1 parent 39efb28 commit 8b8ecb9
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 66 deletions.
11 changes: 2 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,17 +178,10 @@ bin/rails g suspenders:email

### Testing

Set up the project for an in-depth test-driven development workflow.

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

[rspec-rails]: https://github.com/rspec/rspec-rails
[action_dispatch-testing-integration-capybara]: https://github.com/thoughtbot/action_dispatch-testing-integration-capybara
[shoulda-matchers]: https://github.com/thoughtbot/shoulda-matchers
[webdrivers]: https://github.com/titusfortner/webdrivers
[webmock]: https://github.com/bblimke/webmock

## Contributing

Expand Down
9 changes: 6 additions & 3 deletions lib/generators/suspenders/testing_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ def add_gems
end

gem_group :test do
gem "capybara"
gem "action_dispatch-testing-integration-capybara",
github: "thoughtbot/action_dispatch-testing-integration-capybara", tag: "v0.1.0",
github: "thoughtbot/action_dispatch-testing-integration-capybara", tag: "v0.1.1",
require: "action_dispatch/testing/integration/capybara/rspec"
gem "selenium-webdriver"
gem "shoulda-matchers", "~> 6.0"
gem "webdrivers"
gem "webmock"
end

Expand All @@ -29,6 +30,8 @@ def modify_rails_helper
insert_into_file "spec/rails_helper.rb",
"\s\sconfig.infer_base_class_for_anonymous_controllers = false\n",
after: "RSpec.configure do |config|\n"

uncomment_lines "spec/rails_helper.rb", /Rails\.root\.glob/
end

def modify_spec_helper
Expand Down Expand Up @@ -59,7 +62,7 @@ def create_system_spec_dir
end

def configure_chromedriver
copy_file "chromedriver.rb", "spec/support/chromedriver.rb"
copy_file "driver.rb", "spec/support/driver.rb"
end

def configure_i18n_helper
Expand Down
27 changes: 0 additions & 27 deletions lib/generators/templates/testing/chromedriver.rb

This file was deleted.

5 changes: 5 additions & 0 deletions lib/generators/templates/testing/driver.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
RSpec.configure do |config|
config.before(:each, type: :system) do
driven_by :selenium, using: :headless_chrome, screen_size: [1400, 1400]
end
end
36 changes: 9 additions & 27 deletions test/generators/suspenders/testing_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ class TestingGeneratorTest < Rails::Generators::TestCase
end
group :test do
gem "action_dispatch-testing-integration-capybara", github: "thoughtbot/action_dispatch-testing-integration-capybara", tag: "v0.1.0", require: "action_dispatch/testing/integration/capybara/rspec"
gem "capybara"
gem "action_dispatch-testing-integration-capybara", github: "thoughtbot/action_dispatch-testing-integration-capybara", tag: "v0.1.1", require: "action_dispatch/testing/integration/capybara/rspec"
gem "selenium-webdriver"
gem "shoulda-matchers", "~> 6.0"
gem "webdrivers"
gem "webmock"
end
RUBY
Expand Down Expand Up @@ -52,6 +53,7 @@ class TestingGeneratorTest < Rails::Generators::TestCase
assert_file "spec/rails_helper.rb" do |file|
assert_match(/RSpec\.configure do \|config\|\s{3}config\.infer_base_class_for_anonymous_controllers\s*=\s*false/m,
file)
assert_match(/^\#{0}\s*Rails\.root\.glob\(\"spec\/support\/\*\*\/\*\.rb\"\)\.sort\.each { \|f\| require f }/, file)
end
end

Expand Down Expand Up @@ -90,40 +92,18 @@ class TestingGeneratorTest < Rails::Generators::TestCase
end
end

test "configures Chromedriver" do
test "configures driver" do
expected = <<~RUBY
require "selenium/webdriver"
Capybara.register_driver :chrome do |app|
Capybara::Selenium::Driver.new(app, browser: :chrome)
end
Capybara.register_driver :headless_chrome do |app|
options = ::Selenium::WebDriver::Chrome::Options.new
options.headless!
options.add_argument "--window-size=1680,1050"
Capybara::Selenium::Driver.new app,
browser: :chrome,
options: options
end
Capybara.javascript_driver = :headless_chrome
RSpec.configure do |config|
config.before(:each, type: :system) do
driven_by :rack_test
end
config.before(:each, type: :system, js: true) do
driven_by Capybara.javascript_driver
driven_by :selenium, using: :headless_chrome, screen_size: [1400, 1400]
end
end
RUBY

run_generator

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

0 comments on commit 8b8ecb9

Please sign in to comment.