-
-
Notifications
You must be signed in to change notification settings - Fork 530
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
95325e2
commit 762faaa
Showing
8 changed files
with
386 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
module Suspenders | ||
module Generators | ||
class TestingGenerator < Rails::Generators::Base | ||
source_root File.expand_path("../../templates/testing", __FILE__) | ||
desc "Set up the project for an in-depth test-driven development workflow." | ||
|
||
def add_gems | ||
gem_group :development, :test do | ||
gem "rspec-rails", "~> 6.1.0" | ||
end | ||
|
||
gem_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 "shoulda-matchers", "~> 6.0" | ||
gem "webdrivers" | ||
gem "webmock" | ||
end | ||
|
||
Bundler.with_unbundled_env { run "bundle install" } | ||
end | ||
|
||
def run_rspec_installation_script | ||
rails_command "generate rspec:install" | ||
end | ||
|
||
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" | ||
end | ||
|
||
def modify_spec_helper | ||
persistence_file_path = "\s\sconfig.example_status_persistence_file_path = \"tmp/rspec_examples.txt\"\n" | ||
order = "\s\sconfig.order = :random\n\n" | ||
webmock_config = <<~RUBY | ||
WebMock.disable_net_connect!( | ||
allow_localhost: true, | ||
allow: [ | ||
/(chromedriver|storage).googleapis.com/, | ||
"googlechromelabs.github.io", | ||
] | ||
) | ||
RUBY | ||
|
||
insert_into_file "spec/spec_helper.rb", | ||
persistence_file_path + order, | ||
after: "RSpec.configure do |config|\n" | ||
|
||
insert_into_file "spec/spec_helper.rb", "require \"webmock/rspec\"\n\n", before: "RSpec.configure do |config|" | ||
insert_into_file "spec/spec_helper.rb", webmock_config | ||
end | ||
|
||
def create_system_spec_dir | ||
empty_directory "spec/system" | ||
create_file "spec/system/.gitkeep" | ||
end | ||
|
||
def configure_chromedriver | ||
copy_file "chromedriver.rb", "spec/support/chromedriver.rb" | ||
end | ||
|
||
def configure_i18n_helper | ||
copy_file "i18n.rb", "spec/support/i18n.rb" | ||
end | ||
|
||
def configure_shoulda_matchers | ||
copy_file "shoulda_matchers.rb", "spec/support/shoulda_matchers.rb" | ||
end | ||
|
||
def configure_action_mailer_helpers | ||
# https://guides.rubyonrails.org/testing.html#the-basic-test-case | ||
# | ||
# The ActionMailer::Base.deliveries array is only reset automatically in | ||
# ActionMailer::TestCase and ActionDispatch::IntegrationTest tests. If | ||
# you want to have a clean slate outside these test cases, you can reset | ||
# it manually with: ActionMailer::Base.deliveries.clear | ||
copy_file "action_mailer.rb", "spec/support/action_mailer.rb" | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
RSpec.configure do |config| | ||
config.before(:each) do | ||
ActionMailer::Base.deliveries.clear | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
RSpec.configure do |config| | ||
config.include ActionView::Helpers::TranslationHelper | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
Shoulda::Matchers.configure do |config| | ||
config.integrate do |with| | ||
with.test_framework :rspec | ||
with.library :rails | ||
end | ||
end |
Oops, something went wrong.