thoughtbot / thoughtbot/factory_bot

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

Open
#1,459 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Ruby
Stars
8.2k
Forks
2.6k
PR merge metrics
No merged PRs in 30d

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Read GETTING_STARTED.md and compare its factory examples with the non-Rails reproduction in the issue. Document that factories for classes inside modules should use the full namespace, such as class: 'Foo::Bar'. Done when the guide clearly explains the requirement and includes a working example.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
documentation
Issue type
Documentation
Difficulty
1/5
Estimated time
Under an hour
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.