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

Document using string for class when defining a factory #1188

Closed
composerinteralia opened this issue Sep 8, 2018 · 1 comment · Fixed by doorkeeper-gem/doorkeeper#1176
Closed

Comments

@composerinteralia
Copy link
Collaborator

If factory_bot definitions get loaded before the relevant model gets defined (see thoughtbot/factory_bot_rails#264) the factory definition will raise a NameError: uninitialized constant:

factory :access_token, class: Doorkeeper::AccessToken

But using a string or symbol works fine, since we don't try to constantize it until later, when actually running the factory (https://github.com/thoughtbot/factory_bot/blob/master/lib/factory_bot/factory.rb#L22):

factory :access_token, class: "Doorkeeper::AccessToken"

I don't believe we have clear documentation about using a string or symbol for the class.

@composerinteralia
Copy link
Collaborator Author

Also related to #1130

sman591 added a commit to sman591/doorkeeper that referenced this issue Jan 2, 2019
As described in doorkeeper-gem#1043,
factories defined by doorkeeper are unable to be loaded by host apps
using the `factory_bot_rails` gem (receiving errors such as
`uninitialized constant Doorkeeper::AccessGrant`)

Documentation for factory_bot was later added to explain an easy fix for
this: instead of defining the factory's `class: ` with a named constant,
wrap the constant in a string. By doing so, the constant isn't evaluated
until the factory actually runs, at which point the `Doorkeeper::` models
have all been loaded.

Fixes thoughtbot/factory_bot#1188
sman591 added a commit to sman591/doorkeeper that referenced this issue Jan 4, 2019
As described in doorkeeper-gem#1043,
factories defined by doorkeeper are unable to be loaded by host apps
using the `factory_bot_rails` gem (receiving errors such as
`uninitialized constant Doorkeeper::AccessGrant`)

Documentation for factory_bot was later added to explain an easy fix for
this: instead of defining the factory's `class: ` with a named constant,
wrap the constant in a string. By doing so, the constant isn't evaluated
until the factory actually runs, at which point the `Doorkeeper::` models
have all been loaded.

Fixes thoughtbot/factory_bot#1188
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant