Skip to content

Commit

Permalink
Test Cleanup: Refactor tests to use helper
Browse files Browse the repository at this point in the history
Use `with_test_suite` helper to clean up existing test setup.

Also cleans up test setup by using a `file_fixture` that was missed in #1183
  • Loading branch information
stevepolitodesign committed Apr 13, 2024
1 parent 08be1dd commit 2c7d883
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 72 deletions.
127 changes: 69 additions & 58 deletions test/generators/suspenders/factories_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,71 +31,77 @@ class FactoriesGenerator::DefaultTest < Rails::Generators::TestCase
end

test "installs gem with Bundler" do
Bundler.stubs(:with_unbundled_env).yields
generator.expects(:run).with("bundle install").once
with_test_suite :minitest do
Bundler.stubs(:with_unbundled_env).yields
generator.expects(:run).with("bundle install").once

capture(:stdout) do
generator.add_factory_bot
capture(:stdout) do
generator.add_factory_bot
end
end
end

test "removes fixture definitions" do
touch "test/test_helper.rb", content: test_helper

run_generator
with_test_suite :minitest do
run_generator

assert_file app_root("test/test_helper.rb") do |file|
assert_match(/# fixtures :all/, file)
assert_file app_root("test/test_helper.rb") do |file|
assert_match(/# fixtures :all/, file)
end
end
end

test "adds gem to Gemfile" do
run_generator
with_test_suite :minitest do
run_generator

assert_file app_root("Gemfile") do |file|
assert_match(/group :development, :test do\n gem "factory_bot_rails"\nend/, file)
assert_file app_root("Gemfile") do |file|
assert_match(/group :development, :test do\n gem "factory_bot_rails"\nend/, file)
end
end
end

test "includes syntax methods" do
touch "test/test_helper.rb", content: test_helper
with_test_suite :minitest do
run_generator

run_generator

assert_file app_root("test/test_helper.rb") do |file|
assert_match(/class TestCase\n include FactoryBot::Syntax::Methods/, file)
assert_file app_root("test/test_helper.rb") do |file|
assert_match(/class TestCase\n include FactoryBot::Syntax::Methods/, file)
end
end
end

test "creates definition file" do
definition_file = file_fixture("factories.rb").read
with_test_suite :minitest do
definition_file = file_fixture("factories.rb").read

run_generator
run_generator

assert_file app_root("test/factories.rb") do |file|
assert_match definition_file, file
assert_file app_root("test/factories.rb") do |file|
assert_match definition_file, file
end
end
end

test "creates linting test" do
factories_test = file_fixture("factories_test_lint.rb").read
with_test_suite :minitest do
factories_test = file_fixture("factories_test_lint.rb").read

run_generator
run_generator

assert_file app_root("test/factory_bots/factories_test.rb") do |file|
assert_match factories_test, file
assert_file app_root("test/factory_bots/factories_test.rb") do |file|
assert_match factories_test, file
end
end
end

private

def prepare_destination
mkdir "test"
touch "Gemfile"
end

def restore_destination
remove_dir_if_exists "test"
remove_file_if_exists "Gemfile"
remove_dir_if_exists "lib/tasks"
end
Expand All @@ -114,68 +120,73 @@ class FactoriesGenerator::RSpecTest < Rails::Generators::TestCase
teardown :restore_destination

test "includes syntax methods" do
touch("spec/rails_helper.rb")
factory_bot_config = <<~RUBY
FactoryBot.use_parent_strategy = true
with_test_suite :rspec do
touch("spec/rails_helper.rb")
factory_bot_config = <<~RUBY
FactoryBot.use_parent_strategy = true
RSpec.configure do |config|
config.include FactoryBot::Syntax::Methods
end
RUBY
RSpec.configure do |config|
config.include FactoryBot::Syntax::Methods
end
RUBY

run_generator
run_generator

assert_file app_root("spec/support/factory_bot.rb") do |file|
assert_match factory_bot_config, file
end
assert_file app_root("spec/rails_helper.rb") do |file|
assert_match(/Dir\[Rails\.root\.join\("spec\/support\/\*\*\/\*\.rb"\)\]\.sort\.each { \|file\| require file }/, file)
assert_file app_root("spec/support/factory_bot.rb") do |file|
assert_match factory_bot_config, file
end
assert_file app_root("spec/rails_helper.rb") do |file|
assert_match(/Dir\[Rails\.root\.join\("spec\/support\/\*\*\/\*\.rb"\)\]\.sort\.each { \|file\| require file }/, file)
end
end
end

test "creates definition file" do
definition_file = file_fixture("factories.rb").read
with_test_suite :rspec do
definition_file = file_fixture("factories.rb").read

run_generator
run_generator

assert_file app_root("spec/factories.rb") do |file|
assert_match definition_file, file
assert_file app_root("spec/factories.rb") do |file|
assert_match definition_file, file
end
end
end

test "does not modify rails_helper if it's configured to include support files" do
rails_helper = <<~RUBY
Dir[Rails.root.join("spec/support/**/*.rb")].sort.each { |file| require file }
RUBY
touch "spec/rails_helper.rb", content: rails_helper
with_test_suite :rspec do
rails_helper = <<~RUBY
Dir[Rails.root.join("spec/support/**/*.rb")].sort.each { |file| require file }
RUBY
touch "spec/rails_helper.rb", content: rails_helper

run_generator
run_generator

assert_file app_root("spec/rails_helper.rb") do |file|
assert_equal rails_helper, file
assert_file app_root("spec/rails_helper.rb") do |file|
assert_equal rails_helper, file
end
end
end

test "creates linting test" do
factories_spec = file_fixture("factories_spec_lint.rb").read
with_test_suite :rspec do
factories_spec = file_fixture("factories_spec_lint.rb").read

run_generator
run_generator

assert_file app_root("spec/factory_bots/factories_spec.rb") do |file|
assert_match factories_spec, file
assert_file app_root("spec/factory_bots/factories_spec.rb") do |file|
assert_match factories_spec, file
end
end
end

private

def prepare_destination
mkdir "spec"
touch "spec/spec_helper.rb"
touch "Gemfile"
end

def restore_destination
remove_dir_if_exists "spec"
remove_file_if_exists "Gemfile"
remove_dir_if_exists "lib/tasks"
end
Expand Down
15 changes: 1 addition & 14 deletions test/generators/suspenders/testing_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -161,20 +161,7 @@ def rails_helper
end

def spec_helper
# Generated from rails g rspec:install
# Comments removed
<<~RUBY
RSpec.configure do |config|
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
RUBY
file_fixture("spec_helper.rb").read
end
end
end
Expand Down
1 change: 1 addition & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ def with_test_suite(test_suite, &block)
case test_suite
when :minitest
mkdir "test"
touch "test/test_helper.rb", content: file_fixture("test_helper.rb").read
when :rspec
mkdir "spec"
touch "spec/spec_helper.rb"
Expand Down

0 comments on commit 2c7d883

Please sign in to comment.