Skip to content

Commit 762faaa

Browse files
Introduce suspenders:testing generator (#1156)
Set up projects for an in-depth test-driven development workflow. Installs and configures [rspec-rails][], [action_dispatch-testing-integration-capybara][], [shoulda-matchers][], [webdrivers][] and [webmock][]. [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 ## Details Generate `spec/rails_helper.rb` and `spec/spec_helper.rb` via `rails g rspec:intall` in an effort to not drift from what RSpec recommends out of the box. ```ruby #spec/spec_helper.rb RSpec.configure do |config| config.example_status_persistence_file_path = "tmp/rspec_examples.txt" config.order = :random config.expect_with :rspec do |expectations| expectations.include_chain_clauses_in_custom_matcher_descriptions = true end config.mock_with :rspec do |mocks| mocks.verify_partial_doubles = true end config.shared_context_metadata_behavior = :apply_to_host_groups end WebMock.disable_net_connect!( allow_localhost: true, allow: [ /(chromedriver|storage).googleapis.com/, "googlechromelabs.github.io", ] ) ``` The only thing that differs from the existing `spec/rails_helper.rb` configuration is: ```ruby config.infer_base_class_for_anonymous_controllers = false ``` ```ruby # spec/support/chromedriver.rb 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 end end ``` ```ruby # spec/support/shoulda_matchers.rb Shoulda::Matchers.configure do |config| config.integrate do |with| with.test_framework :rspec with.library :rails end end ``` ```ruby # spec/support/i18n.rb RSpec.configure do |config| config.include ActionView::Helpers::TranslationHelper end ``` ```ruby # spec/support/action_mailer.rb RSpec.configure do |config| config.before(:each) do ActionMailer::Base.deliveries.clear end end ``` ## Notable changes This commit removes the [formulaic][] dependency. A follow-up commit could explore creating a separate one-off generator for this, but for now, we're aiming for the leanest build possible. [formulaic]: https://github.com/calebhearth/formulaic
1 parent 95325e2 commit 762faaa

File tree

8 files changed

+386
-0
lines changed

8 files changed

+386
-0
lines changed

NEWS.md

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Unreleased
1414
* Introduce `suspenders:tasks` generator
1515
* Introduce `suspenders:db:migrate` task
1616
* Introduce `suspenders:email` generator
17+
* Introduce `suspenders:testing` generator
1718

1819
20230113.0 (January, 13, 2023)
1920

README.md

+14
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,20 @@ Configures `default_url_options` in `test` and `development`.
176176
bin/rails g suspenders:email
177177
```
178178

179+
### Testing
180+
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][].
186+
187+
[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
192+
179193
## Contributing
180194

181195
See the [CONTRIBUTING] document.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
module Suspenders
2+
module Generators
3+
class TestingGenerator < Rails::Generators::Base
4+
source_root File.expand_path("../../templates/testing", __FILE__)
5+
desc "Set up the project for an in-depth test-driven development workflow."
6+
7+
def add_gems
8+
gem_group :development, :test do
9+
gem "rspec-rails", "~> 6.1.0"
10+
end
11+
12+
gem_group :test do
13+
gem "action_dispatch-testing-integration-capybara",
14+
github: "thoughtbot/action_dispatch-testing-integration-capybara", tag: "v0.1.0",
15+
require: "action_dispatch/testing/integration/capybara/rspec"
16+
gem "shoulda-matchers", "~> 6.0"
17+
gem "webdrivers"
18+
gem "webmock"
19+
end
20+
21+
Bundler.with_unbundled_env { run "bundle install" }
22+
end
23+
24+
def run_rspec_installation_script
25+
rails_command "generate rspec:install"
26+
end
27+
28+
def modify_rails_helper
29+
insert_into_file "spec/rails_helper.rb",
30+
"\s\sconfig.infer_base_class_for_anonymous_controllers = false\n",
31+
after: "RSpec.configure do |config|\n"
32+
end
33+
34+
def modify_spec_helper
35+
persistence_file_path = "\s\sconfig.example_status_persistence_file_path = \"tmp/rspec_examples.txt\"\n"
36+
order = "\s\sconfig.order = :random\n\n"
37+
webmock_config = <<~RUBY
38+
39+
WebMock.disable_net_connect!(
40+
allow_localhost: true,
41+
allow: [
42+
/(chromedriver|storage).googleapis.com/,
43+
"googlechromelabs.github.io",
44+
]
45+
)
46+
RUBY
47+
48+
insert_into_file "spec/spec_helper.rb",
49+
persistence_file_path + order,
50+
after: "RSpec.configure do |config|\n"
51+
52+
insert_into_file "spec/spec_helper.rb", "require \"webmock/rspec\"\n\n", before: "RSpec.configure do |config|"
53+
insert_into_file "spec/spec_helper.rb", webmock_config
54+
end
55+
56+
def create_system_spec_dir
57+
empty_directory "spec/system"
58+
create_file "spec/system/.gitkeep"
59+
end
60+
61+
def configure_chromedriver
62+
copy_file "chromedriver.rb", "spec/support/chromedriver.rb"
63+
end
64+
65+
def configure_i18n_helper
66+
copy_file "i18n.rb", "spec/support/i18n.rb"
67+
end
68+
69+
def configure_shoulda_matchers
70+
copy_file "shoulda_matchers.rb", "spec/support/shoulda_matchers.rb"
71+
end
72+
73+
def configure_action_mailer_helpers
74+
# https://guides.rubyonrails.org/testing.html#the-basic-test-case
75+
#
76+
# The ActionMailer::Base.deliveries array is only reset automatically in
77+
# ActionMailer::TestCase and ActionDispatch::IntegrationTest tests. If
78+
# you want to have a clean slate outside these test cases, you can reset
79+
# it manually with: ActionMailer::Base.deliveries.clear
80+
copy_file "action_mailer.rb", "spec/support/action_mailer.rb"
81+
end
82+
end
83+
end
84+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
RSpec.configure do |config|
2+
config.before(:each) do
3+
ActionMailer::Base.deliveries.clear
4+
end
5+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
require "selenium/webdriver"
2+
3+
Capybara.register_driver :chrome do |app|
4+
Capybara::Selenium::Driver.new(app, browser: :chrome)
5+
end
6+
7+
Capybara.register_driver :headless_chrome do |app|
8+
options = ::Selenium::WebDriver::Chrome::Options.new
9+
options.headless!
10+
options.add_argument "--window-size=1680,1050"
11+
12+
Capybara::Selenium::Driver.new app,
13+
browser: :chrome,
14+
options: options
15+
end
16+
17+
Capybara.javascript_driver = :headless_chrome
18+
19+
RSpec.configure do |config|
20+
config.before(:each, type: :system) do
21+
driven_by :rack_test
22+
end
23+
24+
config.before(:each, type: :system, js: true) do
25+
driven_by Capybara.javascript_driver
26+
end
27+
end
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
RSpec.configure do |config|
2+
config.include ActionView::Helpers::TranslationHelper
3+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Shoulda::Matchers.configure do |config|
2+
config.integrate do |with|
3+
with.test_framework :rspec
4+
with.library :rails
5+
end
6+
end

0 commit comments

Comments
 (0)