How to configure Rails to use subfolders in models with RSpec?

77 Views Asked by At

I have subfolders inside the models folder:

app/models/foos/bar.rb

Autoload paths is configured in application.rb:

module AppName
  class Application < Rails::Application
    config.load_defaults 7.0

    config.public_file_server.enabled = true

    config.autoload_paths += Dir[Rails.root.join('app', 'models', '**/')]

This works fine in development, but not in test environment.

development.rb:

config.cache_classes = false
config.eager_load = false

test.rb

config.cache_classes = false
config.eager_load = false

spec/models/bar_spec.rb

describe Bar do
  ....

rspec raises NameError: uninitialized constant Bar

How to autoload the models in test environment?

Rails 7.0.8

1

There are 1 best solutions below

0
On BEST ANSWER

Haha, it was the .rspec file.

Needs to be

--require rails_helper