rubyatscale / packs-rails

packs-rails establishes and implements a set of conventions for splitting up large monoliths.
MIT License
268 stars 26 forks source link

Classes from pack are not recognized in tests #64

Closed crystal-williams-brown closed 1 year ago

crystal-williams-brown commented 1 year ago

Description I'm attempting to modularize a Rails app. I am starting with one section. I moved the files into a single folder. I installed packwerk and packs-rails. I placed the folder with the files inside of a packs folder at the root of the app. I moved the test files into an rspec folder inside of the section folder. My test fail with the error NameError: uninitialized constant for every single class. All of these classes are in the folder. I don't know why the tests are not treating them like classes.

To Reproduce

  1. move class files into a folder within the packs folder
  2. Add a package.yml turn on engine setting
  3. Add test files and tell rspec to include packs-rails
  4. Run tests and watch them fail

Expected Behaviour I expected the tests to still recognize the class names though they are moved into a pack.

Screenshots This is the package.yml in the pack folder

Screenshot 2023-06-06 at 6 41 00 PM

And here is the one at the root

Screenshot 2023-06-06 at 6 41 12 PM

Here is the file system

Screenshot 2023-06-06 at 6 34 17 PM

Here is the .rspec

Screenshot 2023-06-06 at 6 42 35 PM

Here is one of the failing tests

Screenshot 2023-06-06 at 6 35 43 PM

Version Information

Additional Context I've tried directly requiring packwerk in the files, having the test files specify they are model tests, moving files around, and changing the names of classes. I've also started over from scratch, following the guidance in the instructions and using different tutorials.

ngan commented 1 year ago

Hi, files in lib/* are not autoloaded by Rails, you have to require them yourself (docs).

Idiomatic Rails applications only issue require calls to load stuff from their lib directory, the Ruby standard library, Ruby gems, etc.

As such, lib/* in packs are the same. You'll need to require them yourself at the top of the spec file.

crystal-williams-brown commented 1 year ago

If I move the files out of lib the error remains. I will try adding the require to see if it changes things

crystal-williams-brown commented 1 year ago

I'm not able to require the lib folder in the tests. It says it can't load such files. I just want to understand how this is supposed to work. If I need a different file structure I'll do it. Does this gem help with tests? Maybe I misunderstood the goals of this gem.

crystal-williams-brown commented 1 year ago

I changed the file structure for the pack so that it goes "pack_name < app < models" instead of "pack_name < lib". That fixed the tests!

Screenshot 2023-06-07 at 10 01 58 AM