|
| 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 |
0 commit comments