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

Moving fixtures inside a pack #54

Closed matiaseche closed 1 year ago

matiaseche commented 1 year ago

Hello! I've been experimenting with this gem for a while now, and for the past few days I've been trying to add the test fixtures inside my pack, but with no success :disappointed:

I currently have something like the following:

- app/
- bin/
- config/
- db/
- lib/
- logs/
- packs/
  - test_pack/
    - app/
    - config/
    - test/
      - controllers/
      - factories/
      - fixtures/
        - users.yml # Does not work
      - helpers/
      - jobs/
      - mailers/
      - models/
- public/
- test/
    - controllers/
    - factories/
    - fixtures/
    - helpers/
    - jobs/
    - mailers/
    - models/

If I move the users.yml file to the test/fixtures in the root then it loads correctly, but if I try it inside the pack I get an error where it says undefined method 'users' for #<UserTest ... > after running the test.

Is this something that can't be done with the current state of the gem? I am trying to make several packs since I am working on a very big project, and having the fixtures inside the pack would make it easier to write and check the state of the fixtures that are needed for each pack.

alexevanczuk commented 1 year ago

Hey @matiaseche thanks for opening up this discussion!

I don't believe this is something that is supported out of the box with packs-rails, but it makes a ton of sense for this gem to support Rails fixtures!

I haven't used Rails fixtures before – do you know where/how the directory where they are located is configured, or what API can be used to set that directory? If you wanted to submit a PR to add this functionality I'd love to incorporate it. We could also pair on it and try to get it working together if you wanted.

matiaseche commented 1 year ago

Hey @alexevanczuk, to be honest I don't know much about how fixtures are configured and setup, but I am happy to contribute.

I searched about it, and found that there is a path stored under ActiveSupport::TestCase.fixture_path where the fixtures are read (I got this from the ActiveRecord documentation).

I tried to force it to another path like packs/my_pack/test/fixtures but with no luck. I will continue to make different changes and research, if I come up with something useful I will write it here :wink:

gap777 commented 1 year ago

@matiaseche I was able to do this in ./spec/rails_helper:

config.fixture_path = "#{Rails.root}/packs/applications/media_planner/spec/fixtures"

And use fixtures as I would expect them to be used, from within that pack's tests. However, it appears that the fixtures are loaded by looking in a single location. I don't know that we can configure the fixture_path to be an array of directories.

alexevanczuk commented 1 year ago

Hey folks, since it sounds like there is a limitation in the upstream Rails API, I'd like to close this issue out for now. When/if Rails can get an upstream patch supporting multiple fixture directories, I'd love to have packs-rails automatically configure this to allow packs to store their own fixtures. Until that happens, I'm not sure there's much we can do here to support this.

nathanpalmer commented 1 year ago

Looks like you can use multiple fixture path directories in Rails 7.1

https://api.rubyonrails.org/v7.1.0/classes/ActiveSupport/TestCase.html#method-c-fixture_paths-3D