Skip to content

Commit 372477c

Browse files
committed
Change rails_helper.rb template to friendly error log
This commit will change `rails_helper.rb` template to the following pending migration error log. ## Before ```console % bin/rspec spec/models/foo_spec.rb /Users/koic/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/bundler/gems/rails-0f8568f58d36/activerecord/lib/active_record/migration.rb:578:in `check_pending!': (ActiveRecord::PendingMigrationError) Migrations are pending. To resolve this issue, run: bin/rails db:migrate RAILS_ENV=test from /Users/koic/src/ (snip) /spec/spec_helper.rb:27:in `<top (required)>' from /Users/koic/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/configuration.rb:1455:in `require' from /Users/koic/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/configuration.rb:1455:in `block in requires=' from /Users/koic/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/configuration.rb:1455:in `each' from /Users/koic/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/configuration.rb:1455:in `requires=' from /Users/koic/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/configuration_options.rb:112:in `block in process_options_into' from /Users/koic/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/configuration_options.rb:111:in `each' from /Users/koic/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/configuration_options.rb:111:in `process_options_into' from /Users/koic/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/configuration_options.rb:21:in `configure' from /Users/koic/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/runner.rb:99:in `setup' from /Users/koic/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/runner.rb:86:in `run' from /Users/koic/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/runner.rb:71:in `run' from /Users/koic/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/runner.rb:45:in `invoke' from /Users/koic/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/exe/rspec:4:in `<top (required)>' from bin/rspec:9:in `load' from bin/rspec:9:in `<main>' ``` ## After ```console % bin/rspec spec/models/foo_spec.rb Migrations are pending. To resolve this issue, run: bin/rails db:migrate RAILS_ENV=test ``` This PR got an idea from rails/rails#28939.
1 parent decc627 commit 372477c

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

lib/generators/rspec/install/templates/spec/rails_helper.rb

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,22 @@
2424

2525
<% if RSpec::Rails::FeatureCheck.can_maintain_test_schema? -%>
2626
# Checks for pending migrations and applies them before tests are run.
27-
# If you are not using ActiveRecord, you can remove this line.
28-
ActiveRecord::Migration.maintain_test_schema!
29-
27+
# If you are not using ActiveRecord, you can remove these lines.
28+
begin
29+
ActiveRecord::Migration.maintain_test_schema!
30+
rescue ActiveRecord::PendingMigrationError => e
31+
puts e.to_s.strip
32+
exit 1
33+
end
3034
<% elsif RSpec::Rails::FeatureCheck.can_check_pending_migrations? -%>
3135
# Checks for pending migrations before tests are run.
32-
# If you are not using ActiveRecord, you can remove this line.
33-
ActiveRecord::Migration.check_pending!
34-
36+
# If you are not using ActiveRecord, you can remove these lines.
37+
begin
38+
ActiveRecord::Migration.check_pending!
39+
rescue ActiveRecord::PendingMigrationError => e
40+
puts e.to_s.strip
41+
exit 1
42+
end
3543
<% end -%>
3644
RSpec.configure do |config|
3745
<% if RSpec::Rails::FeatureCheck.has_active_record? -%>

0 commit comments

Comments
 (0)