Skip to content

Commit 4b3e041

Browse files
Test Cleanup: Refactor tests to use helper (#1188)
Use `with_test_suite` helper to clean up existing test setup. This was more relevant when exploring #1156, but is still helpful for generators that are test suite agnostic. Also cleans up test setup by using a `file_fixture` that was missed in #1183
1 parent 5303253 commit 4b3e041

File tree

4 files changed

+93
-98
lines changed

4 files changed

+93
-98
lines changed

test/fixtures/files/rails_helper.rb

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
require "spec_helper"
2+
ENV["RAILS_ENV"] ||= "test"
3+
require_relative "../config/environment"
4+
5+
abort("The Rails environment is running in production mode!") if Rails.env.production?
6+
require "rspec/rails"
7+
8+
# Rails.root.glob("spec/support/**/*.rb").sort.each { |f| require f }
9+
10+
begin
11+
ActiveRecord::Migration.maintain_test_schema!
12+
rescue ActiveRecord::PendingMigrationError => e
13+
abort e.to_s.strip
14+
end
15+
RSpec.configure do |config|
16+
config.fixture_path = "#{::Rails.root}/spec/fixtures"
17+
config.use_transactional_fixtures = true
18+
config.infer_spec_type_from_file_location!
19+
config.filter_rails_from_backtrace!
20+
end
21+
Dir[Rails.root.join("spec/support/**/*.rb")].sort.each { |file| require file }

test/generators/suspenders/factories_generator_test.rb

+69-58
Original file line numberDiff line numberDiff line change
@@ -31,71 +31,77 @@ class FactoriesGenerator::DefaultTest < Rails::Generators::TestCase
3131
end
3232

3333
test "installs gem with Bundler" do
34-
Bundler.stubs(:with_unbundled_env).yields
35-
generator.expects(:run).with("bundle install").once
34+
with_test_suite :minitest do
35+
Bundler.stubs(:with_unbundled_env).yields
36+
generator.expects(:run).with("bundle install").once
3637

37-
capture(:stdout) do
38-
generator.add_factory_bot
38+
capture(:stdout) do
39+
generator.add_factory_bot
40+
end
3941
end
4042
end
4143

4244
test "removes fixture definitions" do
43-
touch "test/test_helper.rb", content: test_helper
44-
45-
run_generator
45+
with_test_suite :minitest do
46+
run_generator
4647

47-
assert_file app_root("test/test_helper.rb") do |file|
48-
assert_match(/# fixtures :all/, file)
48+
assert_file app_root("test/test_helper.rb") do |file|
49+
assert_match(/# fixtures :all/, file)
50+
end
4951
end
5052
end
5153

5254
test "adds gem to Gemfile" do
53-
run_generator
55+
with_test_suite :minitest do
56+
run_generator
5457

55-
assert_file app_root("Gemfile") do |file|
56-
assert_match(/group :development, :test do\n gem "factory_bot_rails"\nend/, file)
58+
assert_file app_root("Gemfile") do |file|
59+
assert_match(/group :development, :test do\n gem "factory_bot_rails"\nend/, file)
60+
end
5761
end
5862
end
5963

6064
test "includes syntax methods" do
61-
touch "test/test_helper.rb", content: test_helper
65+
with_test_suite :minitest do
66+
run_generator
6267

63-
run_generator
64-
65-
assert_file app_root("test/test_helper.rb") do |file|
66-
assert_match(/class TestCase\n include FactoryBot::Syntax::Methods/, file)
68+
assert_file app_root("test/test_helper.rb") do |file|
69+
assert_match(/class TestCase\n include FactoryBot::Syntax::Methods/, file)
70+
end
6771
end
6872
end
6973

7074
test "creates definition file" do
71-
definition_file = file_fixture("factories.rb").read
75+
with_test_suite :minitest do
76+
definition_file = file_fixture("factories.rb").read
7277

73-
run_generator
78+
run_generator
7479

75-
assert_file app_root("test/factories.rb") do |file|
76-
assert_match definition_file, file
80+
assert_file app_root("test/factories.rb") do |file|
81+
assert_match definition_file, file
82+
end
7783
end
7884
end
7985

8086
test "creates linting test" do
81-
factories_test = file_fixture("factories_test_lint.rb").read
87+
with_test_suite :minitest do
88+
factories_test = file_fixture("factories_test_lint.rb").read
8289

83-
run_generator
90+
run_generator
8491

85-
assert_file app_root("test/factory_bots/factories_test.rb") do |file|
86-
assert_match factories_test, file
92+
assert_file app_root("test/factory_bots/factories_test.rb") do |file|
93+
assert_match factories_test, file
94+
end
8795
end
8896
end
8997

9098
private
9199

92100
def prepare_destination
93-
mkdir "test"
94101
touch "Gemfile"
95102
end
96103

97104
def restore_destination
98-
remove_dir_if_exists "test"
99105
remove_file_if_exists "Gemfile"
100106
remove_dir_if_exists "lib/tasks"
101107
end
@@ -114,68 +120,73 @@ class FactoriesGenerator::RSpecTest < Rails::Generators::TestCase
114120
teardown :restore_destination
115121

116122
test "includes syntax methods" do
117-
touch("spec/rails_helper.rb")
118-
factory_bot_config = <<~RUBY
119-
FactoryBot.use_parent_strategy = true
123+
with_test_suite :rspec do
124+
touch("spec/rails_helper.rb")
125+
factory_bot_config = <<~RUBY
126+
FactoryBot.use_parent_strategy = true
120127
121-
RSpec.configure do |config|
122-
config.include FactoryBot::Syntax::Methods
123-
end
124-
RUBY
128+
RSpec.configure do |config|
129+
config.include FactoryBot::Syntax::Methods
130+
end
131+
RUBY
125132

126-
run_generator
133+
run_generator
127134

128-
assert_file app_root("spec/support/factory_bot.rb") do |file|
129-
assert_match factory_bot_config, file
130-
end
131-
assert_file app_root("spec/rails_helper.rb") do |file|
132-
assert_match(/Dir\[Rails\.root\.join\("spec\/support\/\*\*\/\*\.rb"\)\]\.sort\.each { \|file\| require file }/, file)
135+
assert_file app_root("spec/support/factory_bot.rb") do |file|
136+
assert_match factory_bot_config, file
137+
end
138+
assert_file app_root("spec/rails_helper.rb") do |file|
139+
assert_match(/Dir\[Rails\.root\.join\("spec\/support\/\*\*\/\*\.rb"\)\]\.sort\.each { \|file\| require file }/, file)
140+
end
133141
end
134142
end
135143

136144
test "creates definition file" do
137-
definition_file = file_fixture("factories.rb").read
145+
with_test_suite :rspec do
146+
definition_file = file_fixture("factories.rb").read
138147

139-
run_generator
148+
run_generator
140149

141-
assert_file app_root("spec/factories.rb") do |file|
142-
assert_match definition_file, file
150+
assert_file app_root("spec/factories.rb") do |file|
151+
assert_match definition_file, file
152+
end
143153
end
144154
end
145155

146156
test "does not modify rails_helper if it's configured to include support files" do
147-
rails_helper = <<~RUBY
148-
Dir[Rails.root.join("spec/support/**/*.rb")].sort.each { |file| require file }
149-
RUBY
150-
touch "spec/rails_helper.rb", content: rails_helper
157+
with_test_suite :rspec do
158+
rails_helper = <<~RUBY
159+
Dir[Rails.root.join("spec/support/**/*.rb")].sort.each { |file| require file }
160+
RUBY
161+
touch "spec/rails_helper.rb", content: rails_helper
151162

152-
run_generator
163+
run_generator
153164

154-
assert_file app_root("spec/rails_helper.rb") do |file|
155-
assert_equal rails_helper, file
165+
assert_file app_root("spec/rails_helper.rb") do |file|
166+
assert_equal rails_helper, file
167+
end
156168
end
157169
end
158170

159171
test "creates linting test" do
160-
factories_spec = file_fixture("factories_spec_lint.rb").read
172+
with_test_suite :rspec do
173+
factories_spec = file_fixture("factories_spec_lint.rb").read
161174

162-
run_generator
175+
run_generator
163176

164-
assert_file app_root("spec/factory_bots/factories_spec.rb") do |file|
165-
assert_match factories_spec, file
177+
assert_file app_root("spec/factory_bots/factories_spec.rb") do |file|
178+
assert_match factories_spec, file
179+
end
166180
end
167181
end
168182

169183
private
170184

171185
def prepare_destination
172-
mkdir "spec"
173-
touch "spec/spec_helper.rb"
174186
touch "Gemfile"
175187
end
176188

177189
def restore_destination
178-
remove_dir_if_exists "spec"
179190
remove_file_if_exists "Gemfile"
180191
remove_dir_if_exists "lib/tasks"
181192
end

test/generators/suspenders/testing_generator_test.rb

+2-39
Original file line numberDiff line numberDiff line change
@@ -133,48 +133,11 @@ def restore_destination
133133
end
134134

135135
def rails_helper
136-
# Generated from rails g rspec:install
137-
# Comments removed
138-
<<~RUBY
139-
require 'spec_helper'
140-
ENV['RAILS_ENV'] ||= 'test'
141-
require_relative '../config/environment'
142-
143-
abort("The Rails environment is running in production mode!") if Rails.env.production?
144-
require 'rspec/rails'
145-
146-
# Rails.root.glob("spec/support/**/*.rb").sort.each { |f| require f }
147-
148-
begin
149-
ActiveRecord::Migration.maintain_test_schema!
150-
rescue ActiveRecord::PendingMigrationError => e
151-
abort e.to_s.strip
152-
end
153-
RSpec.configure do |config|
154-
config.fixture_path = "#{::Rails.root}/spec/fixtures"
155-
config.use_transactional_fixtures = true
156-
config.infer_spec_type_from_file_location!
157-
config.filter_rails_from_backtrace!
158-
end
159-
Dir[Rails.root.join("spec/support/**/*.rb")].sort.each { |file| require file }
160-
RUBY
136+
file_fixture("rails_helper.rb").read
161137
end
162138

163139
def spec_helper
164-
# Generated from rails g rspec:install
165-
# Comments removed
166-
<<~RUBY
167-
RSpec.configure do |config|
168-
config.expect_with :rspec do |expectations|
169-
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
170-
end
171-
172-
config.mock_with :rspec do |mocks|
173-
mocks.verify_partial_doubles = true
174-
end
175-
config.shared_context_metadata_behavior = :apply_to_host_groups
176-
end
177-
RUBY
140+
file_fixture("spec_helper.rb").read
178141
end
179142
end
180143
end

test/test_helper.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,11 @@ class Application < Rails::Application
8181
restore_file "config/application.rb"
8282
end
8383

84-
# TODO: Refactor existing tests to use this
8584
def with_test_suite(test_suite, &block)
8685
case test_suite
8786
when :minitest
8887
mkdir "test"
88+
touch "test/test_helper.rb", content: file_fixture("test_helper.rb").read
8989
when :rspec
9090
mkdir "spec"
9191
touch "spec/spec_helper.rb"

0 commit comments

Comments
 (0)