Class: Suspenders::Generators::TestingGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/suspenders/testing_generator.rb

Instance Method Summary collapse

Instance Method Details

#add_gemsObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/generators/suspenders/testing_generator.rb', line 33

def add_gems
  gem_group :development, :test do
    gem "rspec-rails", "~> 6.1.0"
  end

  gem_group :test do
    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 "webmock"
  end

  Bundler.with_unbundled_env { run "bundle install" }
end

#configure_action_mailer_helpersObject



102
103
104
105
106
107
108
109
110
# File 'lib/generators/suspenders/testing_generator.rb', line 102

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

#configure_chromedriverObject



90
91
92
# File 'lib/generators/suspenders/testing_generator.rb', line 90

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

#configure_i18n_helperObject



94
95
96
# File 'lib/generators/suspenders/testing_generator.rb', line 94

def configure_i18n_helper
  copy_file "i18n.rb", "spec/support/i18n.rb"
end

#configure_shoulda_matchersObject



98
99
100
# File 'lib/generators/suspenders/testing_generator.rb', line 98

def configure_shoulda_matchers
  copy_file "shoulda_matchers.rb", "spec/support/shoulda_matchers.rb"
end

#create_system_spec_dirObject



85
86
87
88
# File 'lib/generators/suspenders/testing_generator.rb', line 85

def create_system_spec_dir
  empty_directory "spec/system"
  create_file "spec/system/.gitkeep"
end

#modify_rails_helperObject



55
56
57
58
59
60
61
# File 'lib/generators/suspenders/testing_generator.rb', line 55

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

#modify_spec_helperObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/generators/suspenders/testing_generator.rb', line 63

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

#run_rspec_installation_scriptObject



51
52
53
# File 'lib/generators/suspenders/testing_generator.rb', line 51

def run_rspec_installation_script
  rails_command "generate rspec:install"
end