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

Testing non-Rails apps with modules requires factories to define fully namespaced classes #1459

Open
johnpitchko opened this issue Jan 11, 2021 · 2 comments
Labels

Comments

@johnpitchko
Copy link
Contributor

johnpitchko commented Jan 11, 2021

Description

FactoryBot throws an error when defining factories that refer to classes within modules in non-Rails applications. FactoryBot will work, but the class used by the factory must be explicitly stated in the full namespace. Simply updating the GETTING_STARTED.md document should be sufficient.

Reproduction Steps

# bar.rb
module Foo
  class Bar
    ...
  end
end

# factories.rb
FactoryBot.define do
   factory :bar do
    ...
  end
end

Produces this error:
NameError: uninitialized constant Bar

Working Code

# bar.rb
module Foo
  class Bar
    ...
  end
end

# factories.rb
FactoryBot.define do
   factory :bar, class: 'Foo::Bar' do
    ...
  end
end

I'm happy to update GETTING_STARTED.md to explain better and help rookies like me who might get stumped.

@composerinteralia
Copy link
Collaborator

Sure, I'm all for adding a relevant example to the documentation, maybe somewhere around https://github.com/thoughtbot/factory_bot/blob/master/GETTING_STARTED.md#specifying-the-class-explicitly

@kevinrobayna
Copy link

This is particularly confusing when you're trying to build a rails engine. You would think that the rails gem would do the trick but nop 😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants