Skip to content

Commit

Permalink
Update docs to require full namespace
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
johnpitchko committed Sep 21, 2024
1 parent 59b7188 commit 6ea3d55
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions GETTING_STARTED.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,26 @@ It is also possible to explicitly specify the class:
factory :admin, class: "User"
```

Explicit specification of the class, _with the full namespace_, is necessary when defining factories for classes within modules in non-Rails applications:

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

If the full namespace is not provided in the `factory` statement, you will receive a `NameError: uninitialized constant Bar` error.

You can pass a constant as well, if the constant is available (note that this
can cause test performance problems in large Rails applications, since
referring to the constant will cause it to be eagerly loaded).
Expand Down

0 comments on commit 6ea3d55

Please sign in to comment.