Skip to content

Commit bf5164a

Browse files
Update docs to require full namespace (#1669)
* Update docs to require full namespace 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. * docs: stylistic chaanges to content in section on specifying explicit class names --------- Co-authored-by: Valerie Burzynski <[email protected]>
1 parent f14a63e commit bf5164a

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

GETTING_STARTED.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,26 @@ It is also possible to explicitly specify the class:
219219
factory :admin, class: "User"
220220
```
221221

222+
Explicit specification of the class, _with the full namespace_, is necessary when defining factories for classes nested within other modules or classes:
223+
224+
```ruby
225+
# foo/bar.rb
226+
module Foo
227+
class Bar
228+
...
229+
end
230+
end
231+
232+
# factories.rb
233+
FactoryBot.define do
234+
factory :bar, class: 'Foo::Bar' do
235+
...
236+
end
237+
end
238+
```
239+
240+
If the full namespace is not provided in the `factory` statement, you will receive a `NameError: uninitialized constant Bar` error.
241+
222242
You can pass a constant as well, if the constant is available (note that this
223243
can cause test performance problems in large Rails applications, since
224244
referring to the constant will cause it to be eagerly loaded).

0 commit comments

Comments
 (0)