notapatch
notapatch

Reputation: 7183

Convert Rails 6 generator to make Minitest test files after being an RSpec project

Summary

I am converting a Rails project from RSPec to Minitest and want my Rails project generator to create minitest test files.


In detail

I have a Rails project which I generated with skipped tests:

 rails new example --skip-test-unit

I then added RSpec (following the standard rails generate rspec:install). Then I thought I'd like to try minitest instead.

I made a project doppelganger and copied test directory into my rails project:

 rails new example  # so we have test unit now

While I can run minitest tests the generator does not make the files for me.

rails g model Contact --skip-migration

Running via Spring preloader in process 13814
      invoke  active_record
      create    app/models/contact.rb
      # no test files!

I have tried:

How can I fix this to get minitest files generated?

Upvotes: 3

Views: 728

Answers (1)

notapatch
notapatch

Reputation: 7183

I diffed a new application with my current application and application.rb's requires were very different.

App not generating test files

# rails/config/application.rb

require "rails"
# Pick the frameworks you want:
require "active_model/railtie"
require "active_job/railtie"
require "active_record/railtie"
require "active_storage/engine"
require "action_controller/railtie"
require "action_mailer/railtie"
require "action_mailbox/engine"
require "action_text/engine"
require "action_view/railtie"
require "action_cable/engine"
require "sprockets/railtie"
# require "rails/test_unit/railtie" <= generator left test_unit commented out

App Generating test files

# rails/config/application.rb

require "rails/all"

I made the require statements in my non-generating app like the generating app. ran spring stop and ran rails generator which now worked.

rails g model Contact --skip-migration

Running via Spring preloader in process 16286
      invoke  active_record
      create    app/models/contact.rb
      invoke    test_unit
      create      test/models/contact_test.rb
      invoke      factory_bot
      create        test/factories/contacts.rb

Upvotes: 2

Related Questions