Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nested factory references don't play well with the prefix #3

Open
elliotcm opened this issue Jun 8, 2022 · 5 comments
Open

Nested factory references don't play well with the prefix #3

elliotcm opened this issue Jun 8, 2022 · 5 comments
Labels
bug Something isn't working

Comments

@elliotcm
Copy link

elliotcm commented Jun 8, 2022

I don't know for sure if this is an issue since I quickly chose a different option to keep development going, but during my first tries it seemed like the concept of "no prefix for in-engine tests, prefix for apps using the engine" doesn't quite work because if you reference another factory from within your factory (like when it's inferred from an association) then there's a mismatch.

e.g. for an engine called Testing

Factory.define :user do
  office
end
Factory.define :office do
end

When you're working within the engine then creating a :user creates an :office just fine, but outside the engine, creating a :testing_user fails because there's no :office defined (because it's now :testing_office).

Does that jive with your experience or did I mess up somewhere? I can take a look at a fix otherwise.

@zspencer
Copy link
Member

zspencer commented Jun 8, 2022

Can you show me the with_namespace call you're doing? If prefix_optional: true is not set in testing/spec/factories/offices.rb, then :office will only be available as :testing_office.

That said, it would be nice if with_namespace played well with the seam FactoryBot uses to infer associations.

For example:

with_namespace(:Testing, require_prefix: false) do
  define :user do
    office
  end
end

Unpacked to something like:

with_namespace(:Testing, require_prefix: false) do
 define :user do 
   office { association(:office, factory: :testing_office) }
 end
end

So that if you have :office and :testing_office the :testing_user factory always used :testing_office (rather than whichever :office happened to be loaded first.

@zspencer zspencer added the bug Something isn't working label Jun 8, 2022
@elliotcm
Copy link
Author

elliotcm commented Jun 8, 2022

Can you show me the with_namespace call you're doing? If prefix_optional: true is not set in testing/spec/factories/offices.rb, then :office will only be available as :testing_office.

Yeah sorry that was just me sketching the issue out, I got rid of the code but it was something to the effect of with_namespace: ENV.fetch("LOCAL_FACTORIES", "true") == "true"

But again that's just off the top of my head, it wasn't exactly that, but a mechanism to switch prefix off for engine dev.

That said, it would be nice if with_namespace played well with the seam FactoryBot uses to infer associations.

For example:

with_namespace(:Testing, require_prefix: false) do
  define :user do
    office
  end
end

Unpacked to something like:

with_namespace(:Testing, require_prefix: false) do
 define :user do 
   office { association(:office, factory: :testing_office) }
 end
end

So that if you have :office and :testing_office the :testing_user factory always used :testing_office (rather than whichever :office happened to be loaded first.

Yes exactly.

@elliotcm elliotcm changed the title [bug] Nested factory references don't play well with the prefix Nested factory references don't play well with the prefix Jun 9, 2022
@elliotcm
Copy link
Author

elliotcm commented Jun 9, 2022

@zspencer I'm having a little trouble running the tests, is this an activerecord version issue? I'm running through bundler so it's using the versions in the lock file.

  1) FactoryBot::NamespacedFactories has a version number
     Failure/Error: connection.create_table(table_name, &block)

     ArgumentError:
       wrong number of arguments (given 5, expected 1..4)
     # ./.bundle/ruby/3.1.0/gems/activerecord-5.2.8/lib/active_record/connection_adapters/abstract/schema_definitions.rb:263:in `initialize'
     # ./.bundle/ruby/3.1.0/gems/activerecord-5.2.8/lib/active_record/connection_adapters/sqlite3/schema_statements.rb:60:in `new'
     # ./.bundle/ruby/3.1.0/gems/activerecord-5.2.8/lib/active_record/connection_adapters/sqlite3/schema_statements.rb:60:in `create_table_definition'
     # ./.bundle/ruby/3.1.0/gems/activerecord-5.2.8/lib/active_record/connection_adapters/abstract/schema_statements.rb:291:in `create_table'
     # ./spec/spec_helper.rb:45:in `create_table'
     # ./spec/spec_helper.rb:32:in `define_model'
     # ./spec/factory_bot/namespaced_factories_spec.rb:7:in `block (2 levels) in <top (required)>'

Or perhaps a ruby version issue?

@zspencer
Copy link
Member

zspencer commented Jun 9, 2022

I had the same issue on Ruby 3.1; which is why I loosened the Gemfile dependencies.

(Also maybe these should be development dependencies? It's been a while since I've futzed with a Real Gem ™️ rather than just gem(s) in a monolith.)

Note: the Lockfile is not checked in, so you should be able to bundle update and not impact anyone else.

@elliotcm
Copy link
Author

elliotcm commented Jun 9, 2022

(Also maybe these should be development dependencies? It's been a while since I've futzed with a Real Gem ™️ rather than just gem(s) in a monolith.)

Perhaps! Honestly I mostly pretend to know what I'm doing when making gems.

Note: the Lockfile is not checked in, so you should be able to bundle update and not impact anyone else.

👍🏻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants